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
9 changes: 9 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2850,6 +2850,15 @@ void CheckOtherImpl::checkInvalidFree()

const int varIndex = tok->strAt(1) == "(" ? 2 :
tok->strAt(3) == "(" ? 4 : 1;
const Token *op = tok->tokAt(varIndex + 1);
if (std::any_of(op->values().cbegin(),
op->values().cend(),
[&](const ValueFlow::Value &value) {
if (!value.isSymbolicValue() || !value.isKnown() || value.intvalue != 0 || !value.tokvalue)
return false;
return value.tokvalue->str() == "new";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve

The new version fixes the char *q = p - 1; false negative, thanks. But since only new is accepted now, the same false positive is still there for malloc()/free():

void f(void) {
    char *p = malloc(10);
    ++p;
    free(p - 1);   // invalidFree, same as on main
}

Accepting allocation functions here (Token::simpleMatch(value.tokvalue, "(") && mSettings.library.getAllocFuncInfo(value.tokvalue->previous())) is not enough on its own. I tried it, and p - 1 gets no symbolic value at all for malloc, only possible 0 from the failed-allocation path. So this probably needs more valueflow work. If that is out of scope here, maybe add the malloc case as a TODO_ASSERT_EQUALS test, or open a follow-up ticket, so it isn't forgotten.

}))
continue;
Comment thread
ludviggunne marked this conversation as resolved.
const int var1 = tok->tokAt(varIndex)->varId();
const int var2 = tok->tokAt(varIndex + 2)->varId();
const auto alloc1 = utils::as_const(inconclusive).find(var1);
Expand Down
14 changes: 14 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3737,6 +3737,20 @@ static void valueFlowSymbolicOperators(const SymbolDatabase& symboldatabase, con
continue;
if (Token::Match(tok, "<<|>>|/|-") && !astIsLHS(vartok))
continue;
if (Token::Match(tok, "+|-") && constant->intvalue != 0) {
std::unordered_set<nonneg int> ids;
for (const auto &value : vartok->values()) {
if (!value.isSymbolicValue())
continue;
if (!value.tokvalue)
continue;
if (!ids.insert(value.tokvalue->exprId()).second)
continue;
ValueFlow::Value newValue(value);
newValue.intvalue += tok->str() == "-" ? -constant->intvalue : constant->intvalue;
setTokenValue(tok, std::move(newValue), settings);
}
}
if (Token::Match(tok, "<<|>>|^|+|-|%or%") && constant->intvalue != 0)
continue;
if (Token::Match(tok, "*|/") && constant->intvalue != 1)
Expand Down
26 changes: 26 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10070,6 +10070,32 @@ class TestOther : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:2:9]: (style) Variable 'ptr' can be declared as pointer to const [constVariablePointer]\n",
errout_str());

check("void f() {\n"
" char *p = new char[1];\n"
" ++p;\n"
" delete [] (p - 1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:11]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
errout_str());

check("void f() {\n"
" char *p = new char[1];\n"
" delete [] (p - 1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:11]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n"
"[test.cpp:3:5]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset. [invalidFree]\n"
, errout_str());

check("void f() {\n"
" char *p = new char[2];\n"
" char *q = p - 1;\n"
" delete [] (p - 1);\n"
" (void)q;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:11]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n"
"[test.cpp:4:5]: (error) Mismatching address is deleted. The address you get from new must be deleted without offset. [invalidFree]\n",
errout_str());
}

void checkRedundantCopy() {
Expand Down
Loading