Skip to content

Commit 33e18ac

Browse files
AaronBallmantru
authored andcommitted
[Analyzer] No longer crash with VLA operands to unary type traits (#151719)
sizeof was handled correctly, but __datasizeof and _Countof were not. Fixes #151711 (cherry picked from commit 1732748 with adjustments) Dropping the ReleaseNotes part of the original patch.
1 parent 8b6caff commit 33e18ac

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
916916
QualType T = Ex->getTypeOfArgument();
917917

918918
for (ExplodedNode *N : CheckedSet) {
919-
if (Ex->getKind() == UETT_SizeOf) {
919+
if (Ex->getKind() == UETT_SizeOf || Ex->getKind() == UETT_DataSizeOf ||
920+
Ex->getKind() == UETT_CountOf) {
920921
if (!T->isIncompleteType() && !T->isConstantSizeType()) {
921922
assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
922923

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
2+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -x c %s
3+
4+
void clang_analyzer_dump(int);
5+
6+
// Ensure that VLA types are correctly handled by unary type traits in the
7+
// expression engine. Previously, __datasizeof and _Countof both caused failed
8+
// assertions.
9+
void gh151711(int i) {
10+
clang_analyzer_dump(sizeof(int[i++])); // expected-warning {{Unknown}}
11+
#ifdef __cplusplus
12+
// __datasizeof is only available in C++.
13+
clang_analyzer_dump(__datasizeof(int[i++])); // expected-warning {{Unknown}}
14+
#else
15+
// _Countof is only available in C.
16+
clang_analyzer_dump(_Countof(int[i++])); // expected-warning {{Unknown}}
17+
#endif
18+
}

0 commit comments

Comments
 (0)