Skip to content

[clang][lex] Fix lexing malformed pragma within include directive #138165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025

Conversation

sweiglbosker
Copy link
Contributor

@sweiglbosker sweiglbosker commented May 1, 2025

fixes: #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.

Copy link

github-actions bot commented May 1, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 1, 2025
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-clang

Author: Stefan (sweiglbosker)

Changes

fixes: #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 --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);

@sweiglbosker sweiglbosker changed the title [clang][lex] fix lexing malformed pragma within include directive [clang][lex] Fix lexing malformed pragma within include directive May 1, 2025
Copy link

github-actions bot commented May 2, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- clang/lib/Lex/Pragma.cpp
View the diff from clang-format here.
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index 607e7b6a7..ea7eda0d0 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -222,10 +222,8 @@ void Preprocessor::Handle_Pragma(Token &Tok) {
     // Skip bad tokens, and the ')', if present.
     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::eod))
+    while (Tok.isNot(tok::r_paren) && !Tok.isAtStartOfLine() &&
+           Tok.isNot(tok::eof) && Tok.isNot(tok::eod))
       Lex(Tok);
     if (Tok.is(tok::r_paren))
       Lex(Tok);

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix, this needs a test and release note.

Release notes for clang are in clang/docs/ReleaseNotes.rst.

I am not sure what the right file for testing is though @AaronBallman

@AaronBallman
Copy link
Collaborator

Thanks for the fix! Given the nature of the issue, I would probably introduce a new test file rather than modify an existing one. But I think it should live in clang/test/Preprocessor.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, though a few suggested changes.

@sweiglbosker
Copy link
Contributor Author

Done

@AaronBallman AaronBallman merged commit 112291a into llvm:main May 5, 2025
7 checks passed
Copy link

github-actions bot commented May 5, 2025

@sweiglbosker Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@sweiglbosker sweiglbosker deleted the fix-pragma branch May 5, 2025 14:22
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…vm#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 llvm#138094
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…vm#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 llvm#138094
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…vm#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 llvm#138094
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
…vm#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 llvm#138094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category crash-on-invalid
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lexical analysis fails on malformed _Pragma directive after #include
4 participants