[clang] 112291a - [clang][lex] Fix lexing malformed pragma within include directive (#138165)

via cfe-commits cfe-commits at lists.llvm.org
Mon May 5 04:29:45 PDT 2025


Author: Stefan Weigl-Bosker
Date: 2025-05-05T07:29:41-04:00
New Revision: 112291ad35a9b83501ff0f9e89de494d2df17b3c

URL: https://github.com/llvm/llvm-project/commit/112291ad35a9b83501ff0f9e89de494d2df17b3c
DIFF: https://github.com/llvm/llvm-project/commit/112291ad35a9b83501ff0f9e89de494d2df17b3c.diff

LOG: [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

Added: 
    clang/test/Preprocessor/_Pragma-in-include.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Lex/Pragma.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 251b134fb3c7d..599567560f733 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -550,7 +550,9 @@ Bug Fixes in This Version
 - Fixed visibility calculation for template functions. (#GH103477)
 - Fixed a bug where an attribute before a ``pragma clang attribute`` or
   ``pragma clang __debug`` would cause an assertion. Instead, this now diagnoses
-  the invalid attribute location appropriately.  (#GH137861)
+  the invalid attribute location appropriately. (#GH137861)
+- Fixed a crash when a malformed ``_Pragma`` directive appears as part of an 
+  ``#include`` directive. (#GH138094)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 5b6a29bdad910..01c85e6ad95d5 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -220,11 +220,11 @@ void Preprocessor::Handle_Pragma(Token &Tok) {
   if (!tok::isStringLiteral(Tok.getKind())) {
     Diag(PragmaLoc, diag::err__Pragma_malformed);
     // Skip bad tokens, and the ')', if present.
-    if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
+    if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof) && Tok.isNot(tok::eod))
       Lex(Tok);
     while (Tok.isNot(tok::r_paren) &&
            !Tok.isAtStartOfLine() &&
-           Tok.isNot(tok::eof))
+           Tok.isNot(tok::eof) && Tok.isNot(tok::eod))
       Lex(Tok);
     if (Tok.is(tok::r_paren))
       Lex(Tok);

diff  --git a/clang/test/Preprocessor/_Pragma-in-include.c b/clang/test/Preprocessor/_Pragma-in-include.c
new file mode 100644
index 0000000000000..6a121946bf0ce
--- /dev/null
+++ b/clang/test/Preprocessor/_Pragma-in-include.c
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -E %s -verify
+
+// Don't crash, verify that diagnostics are preserved
+#include _Pragma( // expected-error {{_Pragma takes a parenthesized string literal}} expected-error {{expected "FILENAME"}} 


        


More information about the cfe-commits mailing list