Fix 11887: FP knownPointerToBool with const_cast (#5357)

This commit is contained in:
Paul Fultz II 2023-08-22 18:07:49 -05:00 committed by GitHub
parent 6ffe08c9b3
commit 2adaafd20e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View File

@ -743,7 +743,8 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
return {std::move(vtParent)}; return {std::move(vtParent)};
} }
// The return type of a function is not the parent valuetype // 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 {}; return {};
if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) { if (Token::Match(tok->astParent(), "return|(|{|%assign%") && parent) {
*parent = tok->astParent(); *parent = tok->astParent();

View File

@ -1468,8 +1468,12 @@ void CheckCondition::alwaysTrueFalse()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope * scope : symbolDatabase->functionScopes) { for (const Scope * scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { 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; continue;
}
if (!tok->hasKnownIntValue()) if (!tok->hasKnownIntValue())
continue; continue;
if (Token::Match(tok->previous(), "%name% (") && tok->previous()->function()) { if (Token::Match(tok->previous(), "%name% (") && tok->previous()->function()) {

View File

@ -4527,6 +4527,12 @@ private:
ASSERT_EQUALS("[test.cpp:5]: (style) Condition 'i==7' is always false\n" ASSERT_EQUALS("[test.cpp:5]: (style) Condition 'i==7' is always false\n"
"[test.cpp:6]: (style) Condition 'p==nullptr' is always false\n", "[test.cpp:6]: (style) Condition 'p==nullptr' is always false\n",
errout.str()); 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() void alwaysTrueSymbolic()

View File

@ -11362,6 +11362,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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" check("struct A { bool x; };\n"
"bool f(A* a) {\n" "bool f(A* a) {\n"
" if (a) {\n" " if (a) {\n"