Skip to content
Draft
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
11 changes: 2 additions & 9 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2545,17 +2545,10 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti
const Library::ArgumentChecks::Direction argDirection = settings.library.getArgDirection(tok, 1 + argnr, indirect);
if (argDirection == Library::ArgumentChecks::Direction::DIR_IN)
return false;
if (argDirection == Library::ArgumentChecks::Direction::DIR_OUT || argDirection == Library::ArgumentChecks::Direction::DIR_INOUT)
return true;

const bool requireNonNull = settings.library.isnullargbad(tok, 1 + argnr);
if (argDirection == Library::ArgumentChecks::Direction::DIR_OUT ||
argDirection == Library::ArgumentChecks::Direction::DIR_INOUT) {
if (indirect == 0 && isArray(tok1))
return true;
const bool requireInit = settings.library.isuninitargbad(tok, 1 + argnr);
// Assume that if the variable must be initialized then the indirection is 1
if (indirect > 0 && requireInit && requireNonNull)
return true;
}
if (Token::simpleMatch(tok->tokAt(-2), "std :: tie"))
return true;
// if the library says 0 is invalid
Expand Down
2 changes: 1 addition & 1 deletion lib/checkvaarg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void CheckVaarg::va_start_argument()
if (var && var->index() + 2 < function->argCount() && printWarnings) {
auto it = function->argumentList.end();
std::advance(it, -2);
wrongParameterTo_va_start_error(tok, var->name(), it->name()); // cppcheck-suppress derefInvalidIterator // FP due to isVariableChangedByFunctionCall()
wrongParameterTo_va_start_error(tok, var->name(), it->name());
}
tok = tok->linkAt(1);
}
Expand Down
7 changes: 7 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5339,6 +5339,13 @@ class TestStl : public TestFixture {
" return it;\n"
"}\n", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:18:5]: (error, inconclusive) Invalid iterator 'it' used. [eraseDereference]\n", errout_str());

check("int f(const std::vector<int>& v) {\n" // #11895
" auto it = v.end();\n"
" std::advance(it, -2);\n"
" return *it;\n"
"}\n", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("", errout_str());
}

void loopAlgoElementAssign() {
Expand Down