diff --git a/board/main.c b/board/main.c index ac0fda9ba43..58f1c21c78c 100644 --- a/board/main.c +++ b/board/main.c @@ -48,8 +48,8 @@ void set_safety_mode(uint16_t mode, uint16_t param) { // TERMINAL ERROR: we can't continue if SILENT safety mode isn't succesfully set assert_fatal(err == 0, "Error: Failed setting SILENT mode. Hanging\n"); } - safety_tx_blocked = 0; - safety_rx_invalid = 0; + safety_tx_blocked = false; + safety_rx_invalid = false; switch (mode_copy) { case SAFETY_SILENT: @@ -362,9 +362,9 @@ int main(void) { #ifdef DEBUG_FAULTS } else { - led_set(LED_RED, 1); + led_set(LED_RED, true); delay(512000U); - led_set(LED_RED, 0); + led_set(LED_RED, false); delay(512000U); } #endif diff --git a/tests/misra/0001-feat-enforcing-MISRA-compliant-Boolean-values.patch b/tests/misra/0001-feat-enforcing-MISRA-compliant-Boolean-values.patch new file mode 100644 index 00000000000..9e7bd090a3b --- /dev/null +++ b/tests/misra/0001-feat-enforcing-MISRA-compliant-Boolean-values.patch @@ -0,0 +1,105 @@ +diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp +index 1234567..abcdefg 100644 +--- a/lib/checkbool.cpp ++++ b/lib/checkbool.cpp +@@ -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)) +diff --git a/lib/checkbool.h b/lib/checkbool.h +index 1234567..abcdefg 100644 +--- a/lib/checkbool.h ++++ b/lib/checkbool.h +@@ -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*/ +@@ -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(); + +@@ -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); +diff --git a/lib/checkers.cpp b/lib/checkers.cpp +index 1234567..abcdefg 100644 +--- a/lib/checkers.cpp ++++ b/lib/checkers.cpp +@@ -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"}, + diff --git a/tests/misra/install.sh b/tests/misra/install.sh index 9c8fd385c81..245ca64f590 100755 --- a/tests/misra/install.sh +++ b/tests/misra/install.sh @@ -14,5 +14,13 @@ VERS="2.16.0" git fetch --all --tags --force git checkout $VERS +# Apply patch for enforcing MISRA compliant Boolean values +# This patch adds support for catching integer-to-bool assignments +# Ref: https://github.com/danmar/cppcheck/pull/7110 +PATCH_FILE="$DIR/0001-feat-enforcing-MISRA-compliant-Boolean-values.patch" +if [ -f "$PATCH_FILE" ]; then + git apply "$PATCH_FILE" || echo "Warning: Could not apply patch, it may already be applied" +fi + #make clean make MATCHCOMPILTER=yes CXXFLAGS="-O2" -j8