Skip to content

Commit 1732748

Browse files
authored
[Analyzer] No longer crash with VLA operands to unary type traits (#151719)
sizeof was handled correctly, but __datasizeof and _Countof were not. Fixes #151711
1 parent 96d1571 commit 1732748

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ Static Analyzer
241241
---------------
242242
- The Clang Static Analyzer now handles parenthesized initialization.
243243
(#GH148875)
244+
- ``__datasizeof`` (C++) and ``_Countof`` (C) no longer cause a failed assertion
245+
when given an operand of VLA type. (#GH151711)
244246

245247
New features
246248
^^^^^^^^^^^^

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp

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

870870
for (ExplodedNode *N : CheckedSet) {
871-
if (Ex->getKind() == UETT_SizeOf) {
871+
if (Ex->getKind() == UETT_SizeOf || Ex->getKind() == UETT_DataSizeOf ||
872+
Ex->getKind() == UETT_CountOf) {
872873
if (!T->isIncompleteType() && !T->isConstantSizeType()) {
873874
assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
874875

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)