False negative on post-increment of uninitialized variable
| Bugzilla Link | 35419 |
| Resolution | FIXED |
| Resolved on | Nov 30, 2017 01:19 |
| Version | unspecified |
| OS | All |
| CC | @devincoughlin,@LebedevRI |
Extended Description
The analyzer doesn't diagnose when post incrementing an uninitialized variable:
$ cat test_pp_uninit.c void foo() { int p; p++; // A diagnostic should be emitted here }
$ clang -cc1 -analyze -analyzer-checker=core test_pp_uninit.c $
It looks like the UndefinedAssignmentChecker is missing a case for UnaryOperator in its checkBind() method.
This is a great starter bug.
Uggh, fixing this is going to be a bit more involved than I thought.
It looks like ExprEngine::VisitIncrementDecrementOperator() has an early continue when the operand is undefined or unknown:
if (V2_untested.isUnknownOrUndef()) { Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested)); continue; }This causes the analyzer to skip the call to evalStore() and thus, ultimately, skip the checker checkBind() callbacks. This means UndefinedAssignmentChecker::checkBind() will never see the bind.
Since VisitIncrementDecrementOperator() is in the core, fixing it will be a bit more involved. Hopefully all you would need to do is add a call to evalStore() on the early continue path Aha, i see, https://reviews.llvm.org/D40463.
but there may be some fallout from this -- it might not be a fantastic starter bug.
Uggh, fixing this is going to be a bit more involved than I thought.
It looks like ExprEngine::VisitIncrementDecrementOperator() has an early continue when the operand is undefined or unknown:
if (V2_untested.isUnknownOrUndef()) {
Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested));
continue;
}
This causes the analyzer to skip the call to evalStore() and thus, ultimately, skip the checker checkBind() callbacks. This means UndefinedAssignmentChecker::checkBind() will never see the bind.
Since VisitIncrementDecrementOperator() is in the core, fixing it will be a bit more involved. Hopefully all you would need to do is add a call to evalStore() on the early continue path, but there may be some fallout from this -- it might not be a fantastic starter bug.
Additionally, if i replace
if (!val.isUndef())
return;
with
if (const UnaryOperator *U = dyn_cast<UnaryOperator>(StoreE)) {
ProgramStateRef state = C.getState();
if (!state->getSVal(U->getSubExpr(), C.getLocationContext()).isUndef())
return;
assert(!isa<UnaryOperator>(StoreE)); // <- is never hit.
} else if (!val.isUndef())
return;
It looks like the UndefinedAssignmentChecker is missing a case for UnaryOperator in its checkBind() method. So i gave this a stab.
if (!val.isUndef())
return;
^ After that, no UnaryOperator's are left.
This is a great starter bug.
Non-working patch attached. Did not try to look any further yet.
This is tracked internally as rdar://problem/35686936
Added the 'beginner' keyword.
assigned to @devincoughlin