From d6a70d27c7eb8d27646ad3de27ab44d724f91092 Mon Sep 17 00:00:00 2001 From: Tyson Nottingham Date: Fri, 20 Sep 2019 23:24:54 -0700 Subject: [PATCH] Fix false negatives in checkAssignBoolToFloat and minor related improvements (#2198) * Fix false negatives in checkAssignBoolToFloat Detect assignments to expressions involving pointer dereferences, array element accesses, etc. * Pass assignment token to assignBoolToFloatError Pass assignment token rather than boolean token to make error reporting consistent between checkAssignBoolToFloat and checkAssignBoolToPointer, as well as with other assignment checks in the code base. * Make checkAssignBoolToPointer check consistent with checkAssignBoolToFloat --- lib/checkbool.cpp | 24 +++++------------------- test/testbool.cpp | 9 +++++++++ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index 70bcd2581..6f847cdeb 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -289,16 +289,9 @@ void CheckBool::checkAssignBoolToPointer() const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); for (const Scope * scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { - if (tok->str() != "=") - continue; - const ValueType *lhsType = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr; - if (!lhsType || lhsType->pointer == 0) - continue; - const ValueType *rhsType = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr; - if (!rhsType || rhsType->pointer > 0 || rhsType->type != ValueType::Type::BOOL) - continue; - - assignBoolToPointerError(tok); + if (tok->str() == "=" && astIsPointer(tok->astOperand1()) && astIsBool(tok->astOperand2())) { + assignBoolToPointerError(tok); + } } } } @@ -436,15 +429,8 @@ void CheckBool::checkAssignBoolToFloat() const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); for (const Scope * scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { - if (tok->str() == "=" && astIsBool(tok->astOperand2())) { - const Token *lhs = tok->astOperand1(); - while (lhs && (lhs->str() == "." || lhs->str() == "::")) - lhs = lhs->astOperand2(); - if (!lhs || !lhs->variable()) - continue; - const Variable* var = lhs->variable(); - if (var && var->isFloatingType() && !var->isArrayOrPointer()) - assignBoolToFloatError(tok->next()); + if (tok->str() == "=" && astIsFloat(tok->astOperand1(), false) && astIsBool(tok->astOperand2())) { + assignBoolToFloatError(tok); } } } diff --git a/test/testbool.cpp b/test/testbool.cpp index e35acb1f6..c50066d77 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -212,6 +212,15 @@ private: " s.p = true;\n" "}\n"); ASSERT_EQUALS("[test.cpp:6]: (style) Boolean value assigned to floating point variable.\n", errout.str()); + + check("struct S {\n" + " float* p[1];\n" + "};\n" + "void f() {\n" + " S s = {0};\n" + " *s.p[0] = true;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (style) Boolean value assigned to floating point variable.\n", errout.str()); } void comparisonOfBoolExpressionWithInt1() {