diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index cf29a06a7fa..5953dce4c96 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -1179,8 +1179,9 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO continue; const Token* tok3 = tok2->next(); - while (tok3 && tok3->isCast() && tok3->valueType() && - (tok3->valueType()->pointer || + while (tok3 && tok3->isCast() && + (!tok3->valueType() || + tok3->valueType()->pointer || (tok3->valueType()->typeSize(mSettings->platform) == 0) || (tok3->valueType()->typeSize(mSettings->platform) >= mSettings->platform.sizeof_pointer))) tok3 = tok3->astOperand2() ? tok3->astOperand2() : tok3->astOperand1(); diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8dc8e81b0c4..eed6dfe0bf1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1831,6 +1831,11 @@ namespace { }; } +static bool isCastToInteger(const Token* tok) +{ + return tok && tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && tok->valueType()->pointer == 0; +} + void CheckOther::checkConstPointer() { if (!mSettings->severity.isEnabled(Severity::style) && @@ -1883,6 +1888,8 @@ void CheckOther::checkConstPointer() deref = MEMBER; else if (astIsRangeBasedForDecl(tok)) continue; + else if (isCastToInteger(parent)) + continue; if (deref != NONE) { const Token* gparent = parent->astParent(); while (Token::simpleMatch(gparent, "[") && parent != gparent->astOperand2() && parent->str() == gparent->str()) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 5bb96a8cd4d..7925d380ff6 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -7031,7 +7031,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const setValueType(parent, vt); return; } - if (Token::Match(parent->previous(), "%name% (") && parent->astOperand1() == tok && valuetype.pointer > 0U) { + if (Token::Match(parent->tokAt(-1), "%name% (") && !parent->tokAt(-1)->isKeyword() && parent->astOperand1() == tok && valuetype.pointer > 0U) { ValueType vt(valuetype); vt.pointer -= 1U; setValueType(parent, vt); diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index 514f435e4c1..b2c82216e25 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -36,6 +36,7 @@ void validCode(int argInt, GHashTableIter * hash_table_iter, GHashTable * hash_t // cppcheck-suppress checkLibraryNoReturn g_assert_not_reached(); } + // cppcheck-suppress constVariablePointer gpointer p = GINT_TO_POINTER(1); int i = GPOINTER_TO_INT(p); // cppcheck-suppress knownConditionTrueFalse @@ -575,4 +576,4 @@ void g_tree_test() { const GTree *tree2 = g_tree_new((GCompareFunc)g_strcmp0); printf("%p\n", tree2); // cppcheck-suppress memleak -} \ No newline at end of file +} diff --git a/test/testother.cpp b/test/testother.cpp index 3126932038f..e793beb5f3e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4598,6 +4598,19 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n", errout_str()); + + check("uintptr_t f(int* p) {\n" + " return (uintptr_t)p;\n" + "}\n" + "uintptr_t g(int* p) {\n" + " return static_cast(p);\n" + "}\n" + "U h(int* p) {\n" + " return (U)p;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n" + "[test.cpp:4:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n", + errout_str()); } void constArray() {