[clang] [clang][lex] fix lexing malformed pragma within include directive (PR #138165)

via cfe-commits cfe-commits at lists.llvm.org
Thu May 1 09:34:47 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Stefan (sweiglbosker)

<details>
<summary>Changes</summary>

fixes: https://github.com/llvm/llvm-project/issues/138094
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`.

---
Full diff: https://github.com/llvm/llvm-project/pull/138165.diff


1 Files Affected:

- (modified) clang/lib/Lex/Pragma.cpp (+3-2) 


``````````diff
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 5b6a29bdad910..607e7b6a7dceb 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -220,11 +220,12 @@ 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);

``````````

</details>


https://github.com/llvm/llvm-project/pull/138165


More information about the cfe-commits mailing list