Skip to content

Commit fb0ea87

Browse files
committed
[Clang][AST] Fix crash in APValue::LValueBase::getType when we have invalid decl
In some cases when calling APValue::LValueBase::getType() when we have a ValueDecl in some cases we don't handle invalid decls. We iterating over redeclarations we reset the current decl to the current most recent decl and we check the next redeclaration to ensure it is not invalid. Fixes: #69468
1 parent 6e761f3 commit fb0ea87

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ Bug Fixes in This Version
641641
Fixes (`#67317 <https://github.com/llvm/llvm-project/issues/67317>`_)
642642
- Clang now properly diagnoses use of stand-alone OpenMP directives after a
643643
label (including ``case`` or ``default`` labels).
644+
- Fix crash when dealing with ill-formed code where we were not handling invalid
645+
redeclarations properly.
646+
Fixes (`#69468 <https://github.com/llvm/llvm-project/issues/69468>`_)
647+
644648

645649
Before:
646650

clang/lib/AST/APValue.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ QualType APValue::LValueBase::getType() const {
7070
// constexpr int *p = &arr[1]; // valid?
7171
//
7272
// For now, we take the most complete type we can find.
73-
for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl;
73+
for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl());
74+
Redecl && !Redecl->isInvalidDecl();
7475
Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) {
7576
QualType T = Redecl->getType();
7677
if (!T->isIncompleteArrayType())
7778
return T;
79+
D = Redecl;
7880
}
7981
return D->getType();
8082
}

clang/test/AST/gh69468.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -verify %s
2+
3+
4+
a[i] = b[i]; // expected-error {{use of undeclared identifier 'i'}} \
5+
// expected-error {{a type specifier is required for all declarations}} \
6+
// expected-error {{use of undeclared identifier 'b'}} \
7+
// expected-error {{use of undeclared identifier 'i'}}
8+
extern char b[];
9+
extern char a[];
10+
11+
void foo(int j) {
12+
// This used to crash here
13+
a[j] = b[j];
14+
}

0 commit comments

Comments
 (0)