Skip to content

Commit fe492d6

Browse files
Fix #14416 FP knownConditionTrueFalse for function taking const pointer to const (danmar#8140)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 537b099 commit fe492d6

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,9 +2006,7 @@ bool isOppositeExpression(const Token * const tok1, const Token * const tok2, co
20062006
static bool functionModifiesArguments(const Function* f)
20072007
{
20082008
return std::any_of(f->argumentList.cbegin(), f->argumentList.cend(), [](const Variable& var) {
2009-
if (var.isReference() || var.isPointer())
2010-
return !var.isConst();
2011-
return true;
2009+
return var.isReference() && !var.isConst();
20122010
});
20132011
}
20142012

@@ -2089,7 +2087,7 @@ bool isConstFunctionCall(const Token* ftok, const Library& library)
20892087
return false;
20902088
});
20912089
}
2092-
return true;
2090+
return false;
20932091
}
20942092

20952093
bool isConstExpression(const Token *tok, const Library& library)

lib/checksizeof.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void CheckSizeof::checkSizeofForPointerSize()
139139
variable = tok;
140140
else if (tok->strAt(1) == ")" && Token::Match(tok->linkAt(1)->tokAt(-2), "%var% ="))
141141
variable = tok->linkAt(1)->tokAt(-2);
142-
else if (tok->link() && Token::Match(tok, "> ( %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)) && Token::Match(tok->link()->tokAt(-3), "%var% ="))
142+
else if (tok->link() && Token::Match(tok, "> ( %name% (") && Token::Match(tok->link()->tokAt(-3), "%var% ="))
143143
variable = tok->link()->tokAt(-3);
144144
tokSize = tok->tokAt(4);
145145
tokFunc = tok->tokAt(2);

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5895,7 +5895,7 @@ static void checkVariableCallMatch(const Variable* callarg, const Variable* func
58955895
callarg->typeStartToken()->isLong() == funcarg->typeStartToken()->isLong()) {
58965896
same++;
58975897
} else if (callarg->isArrayOrPointer()) {
5898-
if (ptrequals && constEquals && funcarg->typeStartToken()->str() == "void")
5898+
if (ptrequals && constEquals && funcarg->typeStartToken()->str() == "void") // cppcheck-suppress knownConditionTrueFalse // #14418
58995899
fallback1++;
59005900
else if (constEquals && funcarg->isStlStringType() && Token::Match(callarg->typeStartToken(), "char|wchar_t"))
59015901
fallback2++;

test/testcondition.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class TestCondition : public TestFixture {
110110
TEST_CASE(alwaysTrueContainer);
111111
TEST_CASE(alwaysTrueLoop);
112112
TEST_CASE(alwaysTrueTryCatch);
113+
TEST_CASE(alwaysTrueSideEffect);
113114
TEST_CASE(multiConditionAlwaysTrue);
114115
TEST_CASE(duplicateCondition);
115116

@@ -5608,6 +5609,18 @@ class TestCondition : public TestFixture {
56085609
ASSERT_EQUALS("", errout_str());
56095610
}
56105611

5612+
void alwaysTrueSideEffect() {
5613+
check("bool check(const char* const);\n" // #14416
5614+
"void create(const char*);\n"
5615+
"void f(const char* n) {\n"
5616+
" if (!check(n)) {\n"
5617+
" create(n);\n"
5618+
" if (check(n)) {}\n"
5619+
" }\n"
5620+
"}\n");
5621+
ASSERT_EQUALS("", errout_str());
5622+
}
5623+
56115624
void multiConditionAlwaysTrue() {
56125625
check("void f() {\n"
56135626
" int val = 0;\n"

0 commit comments

Comments
 (0)