Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11579 FN knownConditionTrueFalse with non-bool as bool parameter / #9450 string literal to bool conversion in function call #5338

Merged
merged 8 commits into from
Aug 18, 2023

Conversation

chrchr-github
Copy link
Collaborator

No description provided.

@chrchr-github chrchr-github changed the title Fix #11579 FN knownConditionTrueFalse with non-bool as bool parameter Fix #11579 FN knownConditionTrueFalse with non-bool as bool parameter / #9450 string literal to bool conversion in function call Aug 17, 2023
@chrchr-github chrchr-github merged commit 827e87a into danmar:main Aug 18, 2023
72 checks passed
@chrchr-github chrchr-github deleted the chr_Fix11579 branch August 18, 2023 08:32
@@ -1490,6 +1490,8 @@ void CheckCondition::alwaysTrueFalse()
else if (parent->str() == ";" && parent->astParent() && parent->astParent()->astParent() &&
Token::simpleMatch(parent->astParent()->astParent()->previous(), "for ("))
condition = parent->astParent()->astParent()->previous();
else if (isBooleanFuncArg(tok))
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably go into the knownArgument checker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looking at the test cases, knownArgument seems to be more about calculations with a known result, not simple conditions.

Copy link
Contributor

Choose a reason for hiding this comment

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

It checks comparisons as well, but only for integral variables.

@@ -488,7 +488,7 @@ struct ForwardTraversal {
if (allAnalysis.isIncremental())
return Break(Analyzer::Terminate::Bail);
} else if (allAnalysis.isModified()) {
std::vector<ForwardTraversal> ftv = tryForkScope(endBlock, allAnalysis.isModified());
std::vector<ForwardTraversal> ftv = tryForkScope(endBlock, /*isModified*/ true);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a FP. Yes its correct that it is always true, but it makes the code less clear about why it is being passed true.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What if isModified() is expensive to compute?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would probably assign it to a variable and reuse it, but thats not the case here.

if (!mImpl->mValues)
return false;
return std::any_of(mImpl->mValues->begin(), mImpl->mValues->end(), [](const ValueFlow::Value& value) {
return value.isIntValue() && (value.isKnown() || (value.intvalue == 0 && value.isImpossible()));
Copy link
Contributor

Choose a reason for hiding this comment

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

valueFlowInferCondition should make these values known, instead of having the checkers deal with this logic.

@@ -912,7 +912,7 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
}
compileBinOp(tok, state, compileScope);
} else if (tok->str() == "[") {
if (state.cpp && isPrefixUnary(tok, state.cpp) && Token::Match(tok->link(), "] (|{|<")) { // Lambda
if (state.cpp && isPrefixUnary(tok, /*cpp*/ true) && Token::Match(tok->link(), "] (|{|<")) { // Lambda
Copy link
Contributor

Choose a reason for hiding this comment

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

There is already a test case for this with knownArgument, we shouldn't be warning here:

        check("struct A { int x; };"
              "void g(int);\n"
              "void f(int x) {\n"
              "    A y;\n"
              "    y.x = 1;\n"
              "    g(y.x);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

From https://github.com/danmar/cppcheck/blob/main/test/testother.cpp#L10987

Copy link
Collaborator Author

@chrchr-github chrchr-github Aug 18, 2023

Choose a reason for hiding this comment

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

Not quite the same, since this PR is only about bool arguments. The code in the test case still seems weird.
Maybe we should only warn when there is an implicit conversion though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should only warn when there is an implicit conversion though.

Yea the original issue seems to only be about pointer to bool conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants