[clang] fix issue 73559. (PR #74926)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 9 03:10:43 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (ChipsSpectre)

<details>
<summary>Changes</summary>

As stated in this issue, https://github.com/llvm/llvm-project/issues/73559, clang (C version of clang, not C++) crashes when C++ components are parsed.

Even a file which only contains a double-colon "::" leads to this kind of crash.

The reason for this behavior is that TryAnnotateCXXScopeToken() of the clang parser assumes to be executed in C++ mode only. 

But in 2 places in the code, there was no guard to ensure this function is only called in C++ mode. When it is called nonetheless, the aforementioned error is created.

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


2 Files Affected:

- (modified) clang/lib/Parse/ParseDecl.cpp (+2-1) 
- (modified) clang/lib/Parse/ParseDeclCXX.cpp (+1-1) 


``````````diff
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index ece3698967e2f6..5d1c19ae07cb54 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -3483,7 +3483,8 @@ void Parser::ParseDeclarationSpecifiers(
 
     case tok::coloncolon: // ::foo::bar
       // C++ scope specifier.  Annotate and loop, or bail out on error.
-      if (TryAnnotateCXXScopeToken(EnteringContext)) {
+      if (getLangOpts().CPlusPlus &&
+          TryAnnotateCXXScopeToken(EnteringContext)) {
         if (!DS.hasTypeSpecifier())
           DS.SetTypeSpecError();
         goto DoneWithDeclSpec;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 910112ecae964c..eba7ea65beee94 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -2702,7 +2702,7 @@ Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
   bool MalformedTypeSpec = false;
   if (!TemplateInfo.Kind &&
       Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
-    if (TryAnnotateCXXScopeToken())
+    if (getLangOpts().CPlusPlus && TryAnnotateCXXScopeToken())
       MalformedTypeSpec = true;
 
     bool isAccessDecl;

``````````

</details>


https://github.com/llvm/llvm-project/pull/74926


More information about the cfe-commits mailing list