llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

<details>
<summary>Changes</summary>

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
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/139901
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to