Skip to content

[analyzer] Demonstrate superfluous unsigned >= 0 assumption #78442

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

Merged
merged 3 commits into from
Mar 6, 2024
Merged
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
10 changes: 10 additions & 0 deletions clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,16 @@ ConditionBRVisitor::VisitTrueTest(const Expr *Cond, BugReporterContext &BRC,
// previous program state we assuming the newly seen constraint information.
// If we cannot evaluate the condition (and the constraints are the same)
// the analyzer has no information about the value and just assuming it.
// FIXME: This logic is not entirely correct, because e.g. in code like
// void f(unsigned arg) {
// if (arg >= 0) {
// // ...
// }
// }
// it will say that the "arg >= 0" check is _assuming_ something new because
// the constraint that "$arg >= 0" is 1 was added to the list of known
// constraints. However, the unsigned value is always >= 0 so semantically
// this is not a "real" assumption.
bool IsAssuming =
!BRC.getStateManager().haveEqualConstraints(CurrentState, PrevState) ||
CurrentState->getSVal(Cond, LCtx).isUnknownOrUndef();
Expand Down
19 changes: 19 additions & 0 deletions clang/test/Analysis/assuming-unsigned-ge-0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_analyze_cc1 -analyzer-output=text \
// RUN: -analyzer-checker=core -verify %s

int assuming_unsigned_ge_0(unsigned arg) {
// TODO This testcase demonstrates the current incorrect behavior of Clang
// Static Analyzer: here 'arg' is unsigned, so "arg >= 0" is not a fresh
// assumption, but it still appears in the diagnostics as if it's fresh:
// expected-note@+2 {{Assuming 'arg' is >= 0}}
// expected-note@+1 {{Taking false branch}}
if (arg < 0)
return 0;
// expected-note@+2 {{Assuming 'arg' is <= 0}}
// expected-note@+1 {{Taking false branch}}
if (arg > 0)
return 0;
// expected-note@+2 {{Division by zero}}
// expected-warning@+1 {{Division by zero}}
return 100 / arg;
}