Skip to content

Commit 112291a

Browse files
authored
[clang][lex] Fix lexing malformed pragma within include directive (#138165)
this patch fixes a crash triggered by lexing past eof when emitting a diagnostic for a malformed `_Pragma` directive within an `include` directive. Fixed by by preventing the lexer from eating a `tok::eod`. Fixes #138094
1 parent 43c05d9 commit 112291a

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,9 @@ Bug Fixes in This Version
550550
- Fixed visibility calculation for template functions. (#GH103477)
551551
- Fixed a bug where an attribute before a ``pragma clang attribute`` or
552552
``pragma clang __debug`` would cause an assertion. Instead, this now diagnoses
553-
the invalid attribute location appropriately. (#GH137861)
553+
the invalid attribute location appropriately. (#GH137861)
554+
- Fixed a crash when a malformed ``_Pragma`` directive appears as part of an
555+
``#include`` directive. (#GH138094)
554556

555557
Bug Fixes to Compiler Builtins
556558
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Lex/Pragma.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ void Preprocessor::Handle_Pragma(Token &Tok) {
220220
if (!tok::isStringLiteral(Tok.getKind())) {
221221
Diag(PragmaLoc, diag::err__Pragma_malformed);
222222
// Skip bad tokens, and the ')', if present.
223-
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
223+
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof) && Tok.isNot(tok::eod))
224224
Lex(Tok);
225225
while (Tok.isNot(tok::r_paren) &&
226226
!Tok.isAtStartOfLine() &&
227-
Tok.isNot(tok::eof))
227+
Tok.isNot(tok::eof) && Tok.isNot(tok::eod))
228228
Lex(Tok);
229229
if (Tok.is(tok::r_paren))
230230
Lex(Tok);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang_cc1 -E %s -verify
2+
3+
// Don't crash, verify that diagnostics are preserved
4+
#include _Pragma( // expected-error {{_Pragma takes a parenthesized string literal}} expected-error {{expected "FILENAME"}}

0 commit comments

Comments
 (0)