Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[OpenMP] Fix tentative parsing crash with metadirective #139901

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 14, 2025

Conversation

AaronBallman
Copy link
Collaborator

There were two crashes that have the same root cause: not correctly handling unexpected tokens. In one case, we were failing to return early which caused us to parse a paren as a regular token instead of a special token, causing an assertion. The other case was failing to commit or revert the tentative parse action when not getting a paren when one was expected.

Fixes #139665

There were two crashes that have the same root cause: not correctly
handling unexpected tokens. In one case, we were failing to return
early which caused us to parse a paren as a regular token instead of a
special token, causing an assertion. The other case was failing to
commit or revert the tentative parse action when not getting a paren
when one was expected.

Fixes llvm#139665
@AaronBallman AaronBallman added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-invalid clang:openmp OpenMP related changes to Clang labels May 14, 2025
@llvmbot
Copy link
Member

llvmbot commented May 14, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

There were two crashes that have the same root cause: not correctly handling unexpected tokens. In one case, we were failing to return early which caused us to parse a paren as a regular token instead of a special token, causing an assertion. The other case was failing to commit or revert the tentative parse action when not getting a paren when one was expected.

Fixes #139665


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+4)
  • (modified) clang/lib/Parse/ParseOpenMP.cpp (+9-2)
  • (modified) clang/test/OpenMP/metadirective_messages.cpp (+10)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bc13d02e2d20b..f2122d9db603c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -931,6 +931,10 @@ OpenMP Support
 - Fixed a crashing bug with a malformed ``cancel`` directive. (#GH139360)
 - Fixed a crashing bug with ``omp distribute dist_schedule`` if the argument to
   ``dist_schedule`` was not strictly positive. (#GH139266)
+- Fixed two crashing bugs with a malformed ``metadirective`` directive. One was
+  a crash if the next token after ``metadirective`` was a paren, bracket, or
+  brace. The other was if the next token after the meta directive was not an
+  open parenthesis. (#GH139665)
 
 Improvements
 ^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index 85838feae77d3..7995f6d3b0ea7 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2786,8 +2786,12 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
                                    ? OMPC_unknown
                                    : getOpenMPClauseKind(PP.getSpelling(Tok));
       // Check if the clause is unrecognized.
-      if (CKind == OMPC_unknown)
+      if (CKind == OMPC_unknown) {
         Diag(Tok, diag::err_omp_expected_clause) << "metadirective";
+        TPA.Revert();
+        SkipUntil(tok::annot_pragma_openmp_end);
+        return Directive;
+      }
       if (getLangOpts().OpenMP < 52 && CKind == OMPC_otherwise)
         Diag(Tok, diag::err_omp_unexpected_clause)
             << getOpenMPClauseName(CKind) << "metadirective";
@@ -2798,8 +2802,11 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
 
       // Parse '('.
       if (T.expectAndConsume(diag::err_expected_lparen_after,
-                             getOpenMPClauseName(CKind).data()))
+                             getOpenMPClauseName(CKind).data())) {
+        TPA.Revert();
+        SkipUntil(tok::annot_pragma_openmp_end);
         return Directive;
+      }
 
       OMPTraitInfo &TI = Actions.getASTContext().getNewOMPTraitInfo();
       if (CKind == OMPC_when) {
diff --git a/clang/test/OpenMP/metadirective_messages.cpp b/clang/test/OpenMP/metadirective_messages.cpp
index a248e9a4e82a9..9d2934f8b1e10 100644
--- a/clang/test/OpenMP/metadirective_messages.cpp
+++ b/clang/test/OpenMP/metadirective_messages.cpp
@@ -49,3 +49,13 @@ void foo() {
       ;
   #endif
   }
+
+namespace GH139665 {
+void f(){
+#pragma omp metadirective( // expected-error {{expected at least one clause on '#pragma omp metadirective' directive}}
+}
+
+void g() {
+#pragma omp metadirective align // expected-error {{expected '(' after 'align'}}
+}
+} // namespace GH139665

@AaronBallman AaronBallman merged commit 0eb8a92 into llvm:main May 14, 2025
17 checks passed
@AaronBallman AaronBallman deleted the aballman-gh139665 branch May 14, 2025 14:19
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:openmp OpenMP related changes to Clang 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.

[OpenMP] crash on invalid with metadirective
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.