Skip to content

Conversation

@sujalmeshram
Copy link

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.

@github-actions
Copy link

github-actions bot commented Nov 8, 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 Nov 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 8, 2025

@llvm/pr-subscribers-clang

Author: Sujal Meshram (sujalmeshram)

Changes

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

@zyn0217 zyn0217 requested a review from cor3ntin November 9, 2025 16:56
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants