From 58b34f8ec553b7aa667cf95a4fd85a3ffe6f55a1 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Tue, 22 Aug 2023 21:01:36 +0200 Subject: [PATCH] Fix constParameterPointer regression Commit 73251544a ("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. --- lib/checkother.cpp | 9 ++++++++- test/testother.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 05801b4ad15a..d07ce59f99cb 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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)) @@ -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; diff --git a/test/testother.cpp b/test/testother.cpp index b8c48f797a93..4f5209b0d490 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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() {