Skip to content

Commit 5ef9ba0

Browse files
authored
Fix #12833 FP knownConditionTrueFalse regression (#6514)
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)").
1 parent 0bc7aed commit 5ef9ba0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/vf_settokenvalue.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,9 @@ namespace ValueFlow
442442
}
443443

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

test/testcondition.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -4620,6 +4620,17 @@ class TestCondition : public TestFixture {
46204620
" if (j != 0) {}\n"
46214621
"}\n");
46224622
ASSERT_EQUALS("", errout_str());
4623+
4624+
check("void f() {\n"
4625+
" const char *s1 = foo();\n"
4626+
" const char *s2 = bar();\n"
4627+
" if (s2 == NULL)\n"
4628+
" return;\n"
4629+
" size_t len = s2 - s1;\n"
4630+
" if (len == 0)\n"
4631+
" return;\n"
4632+
"}\n");
4633+
ASSERT_EQUALS("", errout_str());
46234634
}
46244635

46254636
void alwaysTrueSymbolic()

0 commit comments

Comments
 (0)