Fix FP incorrectStringBooleanError with unknown macro (#5364)

This commit is contained in:
chrchr-github 2023-08-23 18:06:41 +02:00 committed by GitHub
parent 8cd61941dc
commit 5a7c7b9b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -246,6 +246,21 @@ void CheckString::strPlusCharError(const Token *tok)
reportError(tok, Severity::error, "strPlusChar", "Unusual pointer arithmetic. A value of type '" + charType +"' is added to a string literal.", CWE665, Certainty::normal);
}
static bool isMacroUsage(const Token* tok)
{
if (const Token* parent = tok->astParent()) {
if (parent->isExpandedMacro())
return true;
if (parent->isUnaryOp("!")) {
int argn{};
const Token* ftok = getTokenArgumentFunction(parent, argn);
if (ftok && !ftok->function())
return true;
}
}
return false;
}
//---------------------------------------------------------------------------
// Implicit casts of string literals to bool
// Comparing string literal with strlen() with wrong length
@ -289,8 +304,8 @@ void CheckString::checkIncorrectStringCompare()
}
}
} else if (Token::Match(tok, "%str%|%char%") &&
!(tok->astParent() && tok->astParent()->isExpandedMacro()) &&
isUsedAsBool(tok))
isUsedAsBool(tok) &&
!isMacroUsage(tok))
incorrectStringBooleanError(tok, tok->str());
}
}

View File

@ -785,6 +785,13 @@ private:
" ERROR(\"abc\")\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void g(int, bool);\n"
"void f() {\n"
" MyAssert(!\"abc\");\n"
" g(2, !\"def\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Conversion of string literal \"def\" to bool always evaluates to true.\n", errout.str());
}
void deadStrcmp() {