llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False negative on post-increment of uninitialized variable

Open devincoughlin opened this issue 9 years ago • 8 comments

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.

devincoughlin avatar Nov 25 '17 15:11 devincoughlin

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.

LebedevRI avatar Nov 26 '17 09:11 LebedevRI

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.

devincoughlin avatar Nov 26 '17 01:11 devincoughlin

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;

LebedevRI avatar Nov 25 '17 18:11 LebedevRI

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.

LebedevRI avatar Nov 25 '17 17:11 LebedevRI

broken patch

LebedevRI avatar Nov 25 '17 17:11 LebedevRI

This is tracked internally as rdar://problem/35686936

devincoughlin avatar Nov 25 '17 15:11 devincoughlin

Added the 'beginner' keyword.

devincoughlin avatar Nov 25 '17 15:11 devincoughlin

assigned to @devincoughlin

devincoughlin avatar Nov 25 '17 15:11 devincoughlin