Skip to content

Commit e7e2042

Browse files
authored
Fix crash with invalid VLA in a type trait (llvm#138543)
Transforming an expression to a potentially evaluated expression can fail. If it does so, no longer attempt to make the type trait expression, instead return an error expression. This ensures we don't try to compute the dependence for an invalid type. Fixes llvm#138444
1 parent 8ae9a20 commit e7e2042

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ Non-comprehensive list of changes in this release
287287
stack space when running on Apple AArch64 based platforms. This means that
288288
stack traces of Clang from debuggers, crashes, and profilers may look
289289
different than before.
290+
- Fixed a crash when a VLA with an invalid size expression was used within a
291+
``sizeof`` or ``typeof`` expression. (#GH138444)
290292

291293
New Compiler Flags
292294
------------------

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4700,6 +4700,10 @@ ExprResult Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo,
47004700
TInfo->getType()->isVariablyModifiedType())
47014701
TInfo = TransformToPotentiallyEvaluated(TInfo);
47024702

4703+
// It's possible that the transformation above failed.
4704+
if (!TInfo)
4705+
return ExprError();
4706+
47034707
// C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
47044708
return new (Context) UnaryExprOrTypeTraitExpr(
47054709
ExprKind, TInfo, Context.getSizeType(), OpLoc, R.getEnd());

clang/test/SemaCXX/vla.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@ void func(int expr) {
4141
int array[sizeof(Ty) ? sizeof(Ty{}) : sizeof(int)];
4242
int old_style_assert[expr ? Ty::one : Ty::Neg_one]; // We don't diagnose as a VLA until instantiation
4343
}
44+
45+
namespace GH138444 {
46+
struct S { // expected-note {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const S &' for 1st argument}} \
47+
expected-note {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'S &&' for 1st argument}}
48+
S(const char *); // expected-note {{candidate constructor not viable: no known conversion from 'int' to 'const char *' for 1st argument}}
49+
int size() const;
50+
};
51+
52+
void test() {
53+
S vec1 = 2; // expected-error {{no viable conversion from 'int' to 'S'}}
54+
// Previously, this call to sizeof would cause a crash.
55+
sizeof(int[vec1.size()]);
56+
}
57+
}

0 commit comments

Comments
 (0)