Skip to content

Commit

Permalink
[Clang] Fix crash in Parser::ParseDirectDeclarator by adding check th…
Browse files Browse the repository at this point in the history
…at token is not an annotation token

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: #64836

Differential Revision: https://reviews.llvm.org/D158804
  • Loading branch information
shafik committed Sep 12, 2023
1 parent 063cd55 commit 6eadc8f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Bug Fixes in This Version
(`#65156 <https://github.com/llvm/llvm-project/issues/65156>`_)
- Clang no longer considers the loss of ``__unaligned`` qualifier from objects as
an invalid conversion during method function overload resolution.
- Fix parser crash when dealing with ill-formed objective C++ header code. Fixes
(`#64836 <https://github.com/llvm/llvm-project/issues/64836>`_)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6667,7 +6667,7 @@ void Parser::ParseDirectDeclarator(Declarator &D) {
// 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)
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Parser/gh64836.cpp
Original file line number Diff line number Diff line change
@@ -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@-1 {{expected member name}}
};

0 comments on commit 6eadc8f

Please sign in to comment.