[PATCH] D158804: [Clang] Fix crash in Parser::ParseDirectDeclarator by adding check that token is not an annotation token

Shafik Yaghmour via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 24 19:27:02 PDT 2023


shafik created this revision.
shafik added reviewers: aaron.ballman, nridge, erichkeane.
Herald added a project: All.
shafik requested review of this revision.

In `Parser::ParseDirectDeclarator(...)` in some cases ill-formed code can cause an annotation token to end up where it was not expected. The fix is to add a `!Tok.isAnnotation()` guard before attempting to access identifier info.

This fixes:

https://github.com/llvm/llvm-project/issues/64836


https://reviews.llvm.org/D158804

Files:
  clang/lib/Parse/ParseDecl.cpp
  clang/test/Parser/gh64836.cpp


Index: clang/test/Parser/gh64836.cpp
===================================================================
--- /dev/null
+++ clang/test/Parser/gh64836.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -xobjective-c++-header %s
+
+template <typename, typename>
+class C {};
+
+class B {
+  p // expected-error {{unknown type name 'p'}}
+ private: // expected-error {{'private' is a keyword in Objective-C++}}
+  void f() {} // expected-error {{expected '(' for function-style cast or type construction}}
+  C<int, decltype(f)> c; // expected-error {{use of undeclared identifier 'f'}}
+  // expected-error at -1 {{expected member name}}
+};
Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -6677,7 +6677,7 @@
       // Objective-C++: Detect C++ keywords and try to prevent further errors by
       // treating these keyword as valid member names.
       if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
-          Tok.getIdentifierInfo() &&
+          !Tok.isAnnotation() && Tok.getIdentifierInfo() &&
           Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
              diag::err_expected_member_name_or_semi_objcxx_keyword)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158804.553331.patch
Type: text/x-patch
Size: 1345 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230825/041fdbb0/attachment.bin>


More information about the cfe-commits mailing list