Skip to content

Commit a9f88d4

Browse files
Refs #14294: Fix FN constParameterPointer for cast to integer (#7987)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 1c37d49 commit a9f88d4

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

lib/checkleakautovar.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,8 +1179,9 @@ void CheckLeakAutoVar::ret(const Token *tok, VarInfo &varInfo, const bool isEndO
11791179
continue;
11801180

11811181
const Token* tok3 = tok2->next();
1182-
while (tok3 && tok3->isCast() && tok3->valueType() &&
1183-
(tok3->valueType()->pointer ||
1182+
while (tok3 && tok3->isCast() &&
1183+
(!tok3->valueType() ||
1184+
tok3->valueType()->pointer ||
11841185
(tok3->valueType()->typeSize(mSettings->platform) == 0) ||
11851186
(tok3->valueType()->typeSize(mSettings->platform) >= mSettings->platform.sizeof_pointer)))
11861187
tok3 = tok3->astOperand2() ? tok3->astOperand2() : tok3->astOperand1();

lib/checkother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,11 @@ namespace {
18311831
};
18321832
}
18331833

1834+
static bool isCastToInteger(const Token* tok)
1835+
{
1836+
return tok && tok->isCast() && tok->valueType() && tok->valueType()->isIntegral() && tok->valueType()->pointer == 0;
1837+
}
1838+
18341839
void CheckOther::checkConstPointer()
18351840
{
18361841
if (!mSettings->severity.isEnabled(Severity::style) &&
@@ -1883,6 +1888,8 @@ void CheckOther::checkConstPointer()
18831888
deref = MEMBER;
18841889
else if (astIsRangeBasedForDecl(tok))
18851890
continue;
1891+
else if (isCastToInteger(parent))
1892+
continue;
18861893
if (deref != NONE) {
18871894
const Token* gparent = parent->astParent();
18881895
while (Token::simpleMatch(gparent, "[") && parent != gparent->astOperand2() && parent->str() == gparent->str())

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7033,7 +7033,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
70337033
setValueType(parent, vt);
70347034
return;
70357035
}
7036-
if (Token::Match(parent->previous(), "%name% (") && parent->astOperand1() == tok && valuetype.pointer > 0U) {
7036+
if (Token::Match(parent->tokAt(-1), "%name% (") && !parent->tokAt(-1)->isKeyword() && parent->astOperand1() == tok && valuetype.pointer > 0U) {
70377037
ValueType vt(valuetype);
70387038
vt.pointer -= 1U;
70397039
setValueType(parent, vt);

test/cfg/gtk.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void validCode(int argInt, GHashTableIter * hash_table_iter, GHashTable * hash_t
3636
// cppcheck-suppress checkLibraryNoReturn
3737
g_assert_not_reached();
3838
}
39+
// cppcheck-suppress constVariablePointer
3940
gpointer p = GINT_TO_POINTER(1);
4041
int i = GPOINTER_TO_INT(p);
4142
// cppcheck-suppress knownConditionTrueFalse
@@ -575,4 +576,4 @@ void g_tree_test() {
575576
const GTree *tree2 = g_tree_new((GCompareFunc)g_strcmp0);
576577
printf("%p\n", tree2);
577578
// cppcheck-suppress memleak
578-
}
579+
}

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4598,6 +4598,19 @@ class TestOther : public TestFixture {
45984598
"}\n");
45994599
ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
46004600
errout_str());
4601+
4602+
check("uintptr_t f(int* p) {\n"
4603+
" return (uintptr_t)p;\n"
4604+
"}\n"
4605+
"uintptr_t g(int* p) {\n"
4606+
" return static_cast<uintptr_t>(p);\n"
4607+
"}\n"
4608+
"U h(int* p) {\n"
4609+
" return (U)p;\n"
4610+
"}\n");
4611+
ASSERT_EQUALS("[test.cpp:1:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
4612+
"[test.cpp:4:18]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n",
4613+
errout_str());
46014614
}
46024615

46034616
void constArray() {

0 commit comments

Comments
 (0)