Skip to content
Merged
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
5 changes: 3 additions & 2 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was added in c12cb69 related to function pointers.

vt.pointer -= 1U;
setValueType(parent, vt);
Expand Down
3 changes: 2 additions & 1 deletion test/cfg/gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -575,4 +576,4 @@ void g_tree_test() {
const GTree *tree2 = g_tree_new((GCompareFunc)g_strcmp0);
printf("%p\n", tree2);
// cppcheck-suppress memleak
}
}
13 changes: 13 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uintptr_t>(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() {
Expand Down
Loading