Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang] Fix crash when compiling error with invalid decl #77893

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jcsxky
Copy link
Contributor

@jcsxky jcsxky commented Jan 12, 2024

APValue::LValueBase::LValueBase constructs ValueDecl with its canonicalDecl, even though it's invalid. And when obtain its type, it also check all redecls and ignore checking if it's valid. This will cause crash in invalid code. This patch fixes this issue by checking its validation and skip invalid decls.
Fix issue

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 12, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 12, 2024

@llvm/pr-subscribers-clang

Author: Qizhi Hu (jcsxky)

Changes

APValue::LValueBase::LValueBase constructs ValueDecl with its canonicalDecl, even though it's invalid. And when obtain its type, it also check all redecls and ignore checking if it's valid. This will cause crash in invalid code. This patch fixes this issue by checking its validation and skip invalid decls.
Fix issue


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

2 Files Affected:

  • (modified) clang/lib/AST/APValue.cpp (+6-2)
  • (added) clang/test/AST/invalid-decl-no-crash.cpp (+12)
diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index 4eae308ef5b34c..b1fd93b23f38cc 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -40,7 +40,11 @@ static_assert(
     "Type is insufficiently aligned");
 
 APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
-    : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl()) : nullptr), Local{I, V} {}
+    : Ptr(P ? cast<ValueDecl>(P->getCanonicalDecl()->isInvalidDecl()
+                                  ? P
+                                  : P->getCanonicalDecl())
+            : nullptr),
+      Local{I, V} {}
 APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
     : Ptr(P), Local{I, V} {}
 
@@ -73,7 +77,7 @@ QualType APValue::LValueBase::getType() const {
     for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl;
          Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) {
       QualType T = Redecl->getType();
-      if (!T->isIncompleteArrayType())
+      if (!T->isIncompleteArrayType() && !Redecl->isInvalidDecl())
         return T;
     }
     return D->getType();
diff --git a/clang/test/AST/invalid-decl-no-crash.cpp b/clang/test/AST/invalid-decl-no-crash.cpp
new file mode 100644
index 00000000000000..2b35ef702ae553
--- /dev/null
+++ b/clang/test/AST/invalid-decl-no-crash.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only %s -verify
+
+a[i] = b[i]; // expected-error {{use of undeclared identifier 'i'}} \
+                expected-error {{a type specifier is required for all declarations}} \
+                expected-error {{use of undeclared identifier 'b'}} \
+                expected-error {{use of undeclared identifier 'i'}}
+extern char b[];
+extern char a[];
+
+void foo(int j) {
+  a[j] = b[j];
+}

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.

clang: 18: Assertion `getType(Base)->isPointerType() || getType(Base)->isArrayType()' failed.
2 participants