llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Sujal Meshram (sujalmeshram)

<details>
<summary>Changes</summary>

Fixes a crash when parsing malformed decltype expressions by adding a safety 
check before calling getLastCachedTokenLocation().



Added condition EndLoc != StartLoc to only call getLastCachedTokenLocation() 
when meaningful content was parsed, preventing calls when no cached tokens 
exist.

Also added test case as mentioned in the problem.

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


2 Files Affected:

- (modified) clang/lib/Parse/ParseDeclCXX.cpp (+6) 
- (added) clang/test/Parser/gh165246.cpp (+5) 


``````````diff
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index b96968d4592f5..f9ead20f6dacb 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1132,6 +1132,12 @@ void Parser::AnnotateExistingDecltypeSpecifier(const 
DeclSpec &DS,
   // make sure we have a token we can turn into an annotation token
   if (PP.isBacktrackEnabled()) {
     PP.RevertCachedTokens(1);
+    if (DS.getTypeSpecType() == TST_error) {
+      // make sure we have meaningful cached tokens
+      if (EndLoc.isValid() && StartLoc.isValid() && EndLoc != StartLoc) {
+        EndLoc = PP.getLastCachedTokenLocation();
+      }
+    }
   } else
     PP.EnterToken(Tok, /*IsReinject*/ true);
 
diff --git a/clang/test/Parser/gh165246.cpp b/clang/test/Parser/gh165246.cpp
new file mode 100644
index 0000000000000..40fc622589f25
--- /dev/null
+++ b/clang/test/Parser/gh165246.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -verify %s -std=c++11 -fsyntax-only
+
+int decltype {}
+// expected-error@-1 {{expected '(' after 'decltype'}} \
+// expected-error@-1 {{expected unqualified-id}}
\ No newline at end of file

``````````

</details>


https://github.com/llvm/llvm-project/pull/167114
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to