Skip to content

Commit 6c5d17b

Browse files
Fix #8260 Improve check: Pointer calculation result not null
Signed-off-by: Francois Berder <[email protected]>
1 parent a5ec929 commit 6c5d17b

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/checkcondition.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,15 +1782,39 @@ void CheckCondition::checkPointerAdditionResultNotNull()
17821782
if (tok->isExpandedMacro())
17831783
continue;
17841784

1785-
const Token *calcToken, *exprToken;
1785+
const Token *calcToken = NULL, *exprToken = NULL;
17861786
if (tok->astOperand1()->str() == "+") {
17871787
calcToken = tok->astOperand1();
17881788
exprToken = tok->astOperand2();
17891789
} else if (tok->astOperand2()->str() == "+") {
17901790
calcToken = tok->astOperand2();
17911791
exprToken = tok->astOperand1();
1792-
} else
1793-
continue;
1792+
} else {
1793+
const Token *pointerToken = NULL;
1794+
if (tok->astOperand1()->variable() && tok->astOperand1()->variable()->isPointer())
1795+
pointerToken = tok->astOperand1();
1796+
else if (tok->astOperand2()->variable() && tok->astOperand2()->variable()->isPointer())
1797+
pointerToken = tok->astOperand2();
1798+
1799+
if (!pointerToken)
1800+
continue;
1801+
1802+
const std::list<ValueFlow::Value> &tokenValues = pointerToken->values();
1803+
for (const ValueFlow::Value &val : tokenValues) {
1804+
if (val.isSymbolicValue()) {
1805+
if (val.tokvalue->str() == "+") {
1806+
calcToken = val.tokvalue;
1807+
if (pointerToken == tok->astOperand1())
1808+
exprToken = tok->astOperand2();
1809+
else
1810+
exprToken = tok->astOperand1();
1811+
break;
1812+
}
1813+
}
1814+
}
1815+
if (!calcToken || !exprToken)
1816+
continue;
1817+
}
17941818

17951819
// pointer comparison against NULL (ptr+12==0)
17961820
if (calcToken->hasKnownIntValue())

test/testcondition.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6026,7 +6026,19 @@ class TestCondition : public TestFixture {
60266026
" if (ptr + 1 != 0);\n"
60276027
"}");
60286028
ASSERT_EQUALS("[test.cpp:2:15]: (warning) Comparison is wrong. Result of 'ptr+1' can't be 0 unless there is pointer overflow, and pointer overflow is undefined behaviour. [pointerAdditionResultNotNull]\n", errout_str());
6029-
}
6029+
6030+
// #8260
6031+
check("void f(int *p) {\n"
6032+
" int * q = p + 1;\n"
6033+
" if (q != 0);\n"
6034+
"}");
6035+
ASSERT_EQUALS("[test.cpp:3:9]: (warning) Comparison is wrong. Result of 'p+1' can't be 0 unless there is pointer overflow, and pointer overflow is undefined behaviour. [pointerAdditionResultNotNull]\n", errout_str());
6036+
check("void f(int *p) {\n"
6037+
" int * q = p + 1;\n"
6038+
" if (0 != q);\n"
6039+
"}");
6040+
ASSERT_EQUALS("[test.cpp:3:9]: (warning) Comparison is wrong. Result of 'p+1' can't be 0 unless there is pointer overflow, and pointer overflow is undefined behaviour. [pointerAdditionResultNotNull]\n", errout_str());
6041+
}
60306042

60316043
void duplicateConditionalAssign() {
60326044
setMultiline();

0 commit comments

Comments
 (0)