Skip to content

feat: enforcing MISRA compliant Boolean values #7110

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/checkbool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,57 @@ void CheckBool::assignBoolToFloatError(const Token *tok)
"Boolean value assigned to floating point variable.", CWE704, Certainty::normal);
}

//---------------------------------------------------------------------------
// Check for direct assignment of 0/1 literals to boolean variables
// bool x = 1; -> bool x = true;
// bool y = 0; -> bool y = false;
//---------------------------------------------------------------------------
void CheckBool::checkAssignedLiteralToBoolean()
{
if (!mSettings->severity.isEnabled(Severity::style))
return;

logChecker("CheckBool::checkAssignedLiteralToBoolean"); // style

const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope* scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (tok->str() != "=")
continue;

// Check if left operand is boolean
const Token* varTok = tok->astOperand1();
if (!varTok || !varTok->variable() || !isBool(varTok->variable()))
continue;

// Check if right operand is a numeric literal 0 or 1
const Token* valTok = tok->astOperand2();
if (!valTok || !valTok->isNumber())
continue;

const std::string& value = valTok->str();
if (value == "0" || value == "1") {
assignedLiteralToBooleanError(tok, value);
}
}
}
}

void CheckBool::assignedLiteralToBooleanError(const Token* tok, const std::string& value)
{
const std::string suggestion = (value == "0" ? "false" : "true");
reportError(
tok,
Severity::style,
"assignedLiteralToBoolean",
"Boolean variable assigned a numeric literal '" + value + "'. Consider using '" + suggestion + "' instead.\n"
"Using numeric literals (0 or 1) for boolean assignments is not recommended. "
"Use 'false' or 'true' instead for better code readability and maintainability.",
CWE398,
Certainty::normal
);
}

void CheckBool::returnValueOfFunctionReturningBool()
{
if (!mSettings->severity.isEnabled(Severity::style))
Expand Down
5 changes: 5 additions & 0 deletions lib/checkbool.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class CPPCHECKLIB CheckBool : public Check {
checkBool.checkIncrementBoolean();
checkBool.checkAssignBoolToPointer();
checkBool.checkBitwiseOnBoolean();
checkBool.checkAssignedLiteralToBoolean();
}

/** @brief %Check for comparison of function returning bool*/
Expand All @@ -86,6 +87,9 @@ class CPPCHECKLIB CheckBool : public Check {
/** @brief %Check for using bool in bitwise expression */
void checkBitwiseOnBoolean();

/** @brief %Check for using literal in bool expression */
void checkAssignedLiteralToBoolean();

/** @brief %Check for comparing a bool expression with an integer other than 0 or 1 */
void checkComparisonOfBoolExpressionWithInt();

Expand All @@ -105,6 +109,7 @@ class CPPCHECKLIB CheckBool : public Check {
void assignBoolToPointerError(const Token *tok);
void assignBoolToFloatError(const Token *tok);
void bitwiseOnBooleanError(const Token* tok, const std::string& expression, const std::string& op, bool isCompound = false);
void assignedLiteralToBooleanError(const Token* tok, const std::string& value);
void comparisonOfBoolExpressionWithIntError(const Token *tok, bool not0or1);
void pointerArithBoolError(const Token *tok);
void returnValueBoolError(const Token *tok);
Expand Down
1 change: 1 addition & 0 deletions lib/checkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace checkers {
{"CheckBool::checkComparisonOfBoolWithBool","style,c++"},
{"CheckBool::checkAssignBoolToPointer",""},
{"CheckBool::checkComparisonOfBoolExpressionWithInt","warning"},
{"CheckBool::checkAssignedLiteralToBoolean","style"},
{"CheckBool::pointerArithBool",""},
{"CheckBool::checkAssignBoolToFloat","style,c++"},
{"CheckBool::returnValueOfFunctionReturningBool","style"},
Expand Down