Skip to content

Commit

Permalink
Fix #12833 FP knownConditionTrueFalse regression (#6514)
Browse files Browse the repository at this point in the history
Performing arithmetic on two pointers is not guaranteed to always yield
a non-zero value.

Introduced in commit ac0bd6d ("Fix 12760: FN
knownConditionTrueFalse for pointer + offset (#6491)").
  • Loading branch information
mptre committed Jun 12, 2024
1 parent 0bc7aed commit 5ef9ba0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,9 @@ namespace ValueFlow
}

// Offset of non null pointer is not null also
else if (astIsPointer(tok) && Token::Match(parent, "+|-") && value.isIntValue() && value.isImpossible() &&
value.intvalue == 0) {
else if (astIsPointer(tok) && Token::Match(parent, "+|-") &&
(parent->astOperand2() == nullptr || !astIsPointer(parent->astOperand2())) &&
value.isIntValue() && value.isImpossible() && value.intvalue == 0) {
setTokenValue(parent, value, settings);
}

Expand Down
11 changes: 11 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4620,6 +4620,17 @@ class TestCondition : public TestFixture {
" if (j != 0) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f() {\n"
" const char *s1 = foo();\n"
" const char *s2 = bar();\n"
" if (s2 == NULL)\n"
" return;\n"
" size_t len = s2 - s1;\n"
" if (len == 0)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void alwaysTrueSymbolic()
Expand Down

0 comments on commit 5ef9ba0

Please sign in to comment.