Skip to content

Commit

Permalink
Fix 11887: FP knownPointerToBool with const_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 committed Aug 21, 2023
1 parent 59c3bd2 commit d13f794
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
return {std::move(vtParent)};
}
// The return type of a function is not the parent valuetype
if (Token::simpleMatch(tok->astParent(), "(") && ftok && !tok->isCast() && ftok->tokType() != Token::eType)
if (Token::simpleMatch(tok->astParent(), "(") && ftok && !tok->astParent()->isCast() && ftok->tokType() != Token::eType)
return {};
if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) {
*parent = tok->astParent();
Expand Down
5 changes: 4 additions & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,11 @@ void CheckCondition::alwaysTrueFalse()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope * scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (Token::simpleMatch(tok, "<") && tok->link()) // don't write false positives when templates are used
// don't write false positives when templates are used or inside of asserts or non-evaluated contexts
if (tok->link() && (Token::simpleMatch(tok, "<") || Token::Match(tok->previous(), "static_assert|assert|ASSERT|sizeof|decltype ("))) {
tok = tok->link();
continue;
}
if (!tok->hasKnownIntValue())
continue;
if (Token::Match(tok->previous(), "%name% (") && tok->previous()->function()) {
Expand Down
6 changes: 6 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,12 @@ class TestCondition : public TestFixture {
ASSERT_EQUALS("[test.cpp:5]: (style) Condition 'i==7' is always false\n"
"[test.cpp:6]: (style) Condition 'p==nullptr' is always false\n",
errout.str());

check("enum E { E0, E1 };\n"
"void f() {\n"
" static_assert(static_cast<int>(E::E1) == 1);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void alwaysTrueSymbolic()
Expand Down
6 changes: 6 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11362,6 +11362,12 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f() {\n"
" const int* x = nullptr;\n"
" std::empty(const_cast<int*>(x));\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("struct A { bool x; };\n"
"bool f(A* a) {\n"
" if (a) {\n"
Expand Down

0 comments on commit d13f794

Please sign in to comment.