Fixed #2821 (New Check : bool pointer null truth assignment)

This commit is contained in:
Daniel Marjamäki 2011-07-28 07:28:24 +02:00
parent 4258705ce3
commit d2c0e5e7e6
3 changed files with 44 additions and 0 deletions

View File

@ -3835,3 +3835,27 @@ void CheckOther::duplicateBreakError(const Token *tok)
"Consecutive break or continue statements are unnecessary\n"
"The second of the two statements can never be executed, and so should be removed\n");
}
void CheckOther::checkAssignBoolToPointer()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "[;{}] %var% = %bool% ;"))
{
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const Variable *var1(symbolDatabase->getVariableFromVarId(tok->next()->varId()));
// Is variable a pointer?
if (var1 && var1->nameToken()->strAt(-1) == "*")
assignBoolToPointerError(tok->next());
}
}
}
void CheckOther::assignBoolToPointerError(const Token *tok)
{
reportError(tok, Severity::error, "assignBoolToPointer",
"Assigning bool value to pointer (converting bool value to address)");
}

View File

@ -101,6 +101,8 @@ public:
checkOther.checkComparisonOfBoolWithInt();
checkOther.checkSwitchCaseFallThrough();
checkOther.checkAlwaysTrueOrFalseStringCompare();
checkOther.checkAssignBoolToPointer();
}
/** @brief Clarify calculation for ".. a * b ? .." */
@ -232,6 +234,9 @@ public:
bool isRecordTypeWithoutSideEffects(const Token *tok);
/** @brief assigning bool to pointer */
void checkAssignBoolToPointer();
// Error messages..
void cstyleCastError(const Token *tok);
void dangerousUsageStrtolError(const Token *tok);
@ -266,12 +271,14 @@ public:
void duplicateExpressionError(const Token *tok1, const Token *tok2, const std::string &op);
void alwaysTrueFalseStringCompare(const Token *tok, const std::string& str1, const std::string& str2);
void duplicateBreakError(const Token *tok);
void assignBoolToPointerError(const Token *tok);
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings)
{
CheckOther c(0, settings, errorLogger);
// error
c.assignBoolToPointerError(0);
c.sprintfOverlappingDataError(0, "varname");
c.udivError(0);
c.zerodivError(0);
@ -328,6 +335,7 @@ public:
return "Other checks\n"
// error
"* Assigning bool value to pointer (converting bool value to address)"
"* [[OverlappingData|bad usage of the function 'sprintf' (overlapping data)]]\n"
"* division with zero\n"
"* using fflush() on an input stream\n"

View File

@ -35,6 +35,8 @@ private:
void run()
{
TEST_CASE(assignBoolToPointer);
TEST_CASE(zeroDiv1);
TEST_CASE(zeroDiv2);
TEST_CASE(zeroDiv3);
@ -171,6 +173,7 @@ private:
checkOther.checkIncrementBoolean();
checkOther.checkComparisonOfBoolWithInt();
checkOther.checkDuplicateBreak();
checkOther.checkAssignBoolToPointer();
}
class SimpleSuppressor: public ErrorLogger
@ -229,6 +232,15 @@ private:
}
void assignBoolToPointer()
{
check("void foo(bool *p) {\n"
" p = false;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Assigning bool value to pointer (converting bool value to address)\n", errout.str());
}
void zeroDiv1()
{
check("void foo()\n"