Skip to content

[Analyzer] No longer crash with VLA operands to unary type traits #154738

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

Open
wants to merge 1 commit into
base: release/21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
QualType T = Ex->getTypeOfArgument();

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

Expand Down
18 changes: 18 additions & 0 deletions clang/test/Analysis/engine/gh151711.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify -x c %s

void clang_analyzer_dump(int);

// Ensure that VLA types are correctly handled by unary type traits in the
// expression engine. Previously, __datasizeof and _Countof both caused failed
// assertions.
void gh151711(int i) {
clang_analyzer_dump(sizeof(int[i++])); // expected-warning {{Unknown}}
#ifdef __cplusplus
// __datasizeof is only available in C++.
clang_analyzer_dump(__datasizeof(int[i++])); // expected-warning {{Unknown}}
#else
// _Countof is only available in C.
clang_analyzer_dump(_Countof(int[i++])); // expected-warning {{Unknown}}
#endif
}
Loading