Skip to content

Commit 2c6098d

Browse files
Fix #14303 FN knownPointerToBool for function pointer / #13441 FN knownConditionTrueFalse (Compare function pointer with nullptr) (#8886)
1 parent 91941c2 commit 2c6098d

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎lib/checkother.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4446,7 +4446,7 @@ void CheckOtherImpl::checkKnownPointerToBool()
44464446
for (const Token* tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
44474447
if (!tok->hasKnownIntValue())
44484448
continue;
4449-
if (!astIsPointer(tok))
4449+
if (!astIsPointer(tok) && !tok->function())
44504450
continue;
44514451
if (Token::Match(tok->astParent(), "?|!|&&|%oror%|%comp%"))
44524452
continue;

‎lib/valueflow.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,10 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
11781178
ValueFlow::Value value{0};
11791179
value.setImpossible();
11801180
setTokenValue(tok, std::move(value), settings);
1181+
} else if (tok->function() && tok->scope()->isExecutable()) {
1182+
ValueFlow::Value value{0};
1183+
value.setImpossible();
1184+
setTokenValue(tok, std::move(value), settings);
11811185
}
11821186
}
11831187
}
@@ -5162,7 +5166,8 @@ static void valueFlowInferCondition(TokenList& tokenlist, const Settings& settin
51625166
}
51635167
} else if (Token::Match(tok->astParent(), "?|&&|!|%oror%") ||
51645168
Token::Match(tok->astParent()->previous(), "if|while (") ||
5165-
(astIsPointer(tok) && isUsedAsBool(tok, settings))) {
5169+
(astIsPointer(tok) && isUsedAsBool(tok, settings)) ||
5170+
tok->function()) {
51665171
std::vector<ValueFlow::Value> result = infer(makeIntegralInferModel(), "!=", tok->values(), 0);
51675172
if (result.size() != 1)
51685173
continue;

‎test/testcondition.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5509,6 +5509,19 @@ class TestCondition : public TestFixture {
55095509
" std::size_t x;\n"
55105510
"};\n");
55115511
ASSERT_EQUALS("", errout_str());
5512+
5513+
check("int g();\n" // #13441
5514+
"void f() {\n"
5515+
" int (*p)() = g;\n"
5516+
" if (p == nullptr) {}\n"
5517+
" if (g == nullptr) {}\n"
5518+
" auto q = g;\n"
5519+
" if (q == nullptr) {}\n"
5520+
"};\n");
5521+
ASSERT_EQUALS("[test.cpp:4:11]: (style) Condition 'p==nullptr' is always false [knownConditionTrueFalse]\n"
5522+
"[test.cpp:5:11]: (style) Condition 'g==nullptr' is always false [knownConditionTrueFalse]\n"
5523+
"[test.cpp:7:11]: (style) Condition 'q==nullptr' is always false [knownConditionTrueFalse]\n",
5524+
errout_str());
55125525
}
55135526

55145527
void alwaysTrueContainer() {

‎test/testother.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14072,6 +14072,12 @@ class TestOther : public TestFixture {
1407214072
" g(b);\n"
1407314073
"}\n");
1407414074
ASSERT_EQUALS("", errout_str());
14075+
14076+
check("void g(bool);\n" // #14303
14077+
"void f() {\n"
14078+
" g(g);\n"
14079+
"}\n");
14080+
ASSERT_EQUALS("[test.cpp:3:7]: (style) Pointer expression 'g' converted to bool is always true. [knownPointerToBool]\n", errout_str());
1407514081
}
1407614082

1407714083
void iterateByValue() {

0 commit comments

Comments
 (0)