https://github.com/sujalmeshram created 
https://github.com/llvm/llvm-project/pull/167114

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.

>From 26326caff9c497bba23676106db5146ff9d80faf Mon Sep 17 00:00:00 2001
From: Sujal Meshram <[email protected]>
Date: Sat, 8 Nov 2025 09:32:50 +0000
Subject: [PATCH] Fix: Issue #165246

---
 clang/lib/Parse/ParseDeclCXX.cpp | 6 ++++++
 clang/test/Parser/gh165246.cpp   | 5 +++++
 2 files changed, 11 insertions(+)
 create mode 100644 clang/test/Parser/gh165246.cpp

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

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

Reply via email to