Skip to content

Commit

Permalink
Fix constParameterPointer regression
Browse files Browse the repository at this point in the history
Commit 7325154 ("Fix #11842 FN constParameterPointer with library function
(#5257)") most likely introduced a regression for (C) function pointers passed
to functions provided by the standard library that cppcheck has knowledge about.
  • Loading branch information
mptre committed Aug 22, 2023
1 parent 03b952d commit 58b34f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,13 @@ static const Token* getVariableChangedStart(const Variable* p)
return start;
}

static bool isFunctionPointer(const Token *tok)
{
if (!tok || !tok->next())
return false;
return Token::Match(tok->next(), "( * %var% )");
}

void CheckOther::checkConstPointer()
{
if (!mSettings->severity.isEnabled(Severity::style))
Expand Down Expand Up @@ -1660,7 +1667,7 @@ void CheckOther::checkConstPointer()
continue;
}
}
} else {
} else if (!isFunctionPointer(var->typeStartToken())) {
const auto dir = mSettings->library.getArgDirection(ftok, argn + 1);
if (dir == Library::ArgumentChecks::Direction::DIR_IN)
continue;
Expand Down
6 changes: 6 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3808,6 +3808,12 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'p' can be declared as pointer to const\n",
errout.str());

check("void f(void *p, size_t nmemb, size_t size, int (*cmp)(const void *, const void *)) {\n"
" qsort(p, nmemb, size, cmp);\n"
"}\n");
ASSERT_EQUALS("",
errout.str());
}

void switchRedundantAssignmentTest() {
Expand Down

0 comments on commit 58b34f8

Please sign in to comment.