Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1782,15 +1782,39 @@ void CheckCondition::checkPointerAdditionResultNotNull()
if (tok->isExpandedMacro())
continue;

const Token *calcToken, *exprToken;
const Token *calcToken = nullptr, *exprToken = nullptr;
if (tok->astOperand1()->str() == "+") {
calcToken = tok->astOperand1();
exprToken = tok->astOperand2();
} else if (tok->astOperand2()->str() == "+") {
calcToken = tok->astOperand2();
exprToken = tok->astOperand1();
} else
continue;
} else {
const Token *pointerToken = nullptr;
if (tok->astOperand1()->variable() && tok->astOperand1()->variable()->isPointer())
pointerToken = tok->astOperand1();
else if (tok->astOperand2()->variable() && tok->astOperand2()->variable()->isPointer())
pointerToken = tok->astOperand2();

if (!pointerToken)
continue;

const std::list<ValueFlow::Value> &tokenValues = pointerToken->values();
for (const ValueFlow::Value &val : tokenValues) {
if (val.isSymbolicValue()) {
if (val.tokvalue->str() == "+") {
calcToken = val.tokvalue;
if (pointerToken == tok->astOperand1())
exprToken = tok->astOperand2();
else
exprToken = tok->astOperand1();
break;
}
}
}
if (!calcToken || !exprToken)
continue;
}

// pointer comparison against NULL (ptr+12==0)
if (calcToken->hasKnownIntValue())
Expand Down
12 changes: 12 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6026,6 +6026,18 @@ class TestCondition : public TestFixture {
" if (ptr + 1 != 0);\n"
"}");
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());

// #8260
check("void f(int *p) {\n"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a test for the original example from #8260?

" int * q = p + 1;\n"
" if (q != 0);\n"
"}");
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());
check("void f(int *p) {\n"
" int * q = p + 1;\n"
" if (0 != q);\n"
"}");
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());
}

void duplicateConditionalAssign() {
Expand Down
Loading