Menu

False negative: always-true logic hidden behind boolean temporaries

Rodri
2026-07-28
2026-07-28
  • Rodri

    Rodri - 2026-07-28

    Hi, I found a false negative regarding the rule incorrectLogicOperator.

    Cppcheck reports incorrectLogicOperator for the direct condition x >= 0 || x <= 10, because the disjunction is always true. However, if the two comparisons are stored in boolean temporaries first, cppcheck reports no warning even though the condition is semantically equivalent.

    Original seed program

    static void foo(int x) {
        if (x >= 0 || x <= 10) {}
    }
    

    Reproducing program

    #include <stdbool.h>
    
    static void foo(int x) {
      bool b1 = x >= 0;
      bool b2 = x <= 10;
      if (b1 || b2) {
      }
    }
    

    The condition b1 || b2 is equivalent to x >= 0 || x <= 10, which is always true for any integer x.

    Actual result

    Cppcheck reports no incorrectLogicOperator warning for the reproducing program.

    Expected result

    Cppcheck should report incorrectLogicOperator for the condition b1 || b2.

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all incorrect_logic_operator_bool_temps.c
    

    Version: 2.21.0

     
  • CHR

    CHR - 2026-07-28

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14943

     

Log in to post a comment.