Menu

False Positive: knownConditionTrueFalse with unsigned integers

2023-09-29
2023-09-29
  • David Gibson

    David Gibson - 2023-09-29

    With --enable=all 2.12.0, 2.12.1 and current git (6773cdb34bb4697a41c4438ebb2a34c4e4de4b69) all report knownConditionTrueFalse on the following code:

    #define MIN(x, y)       (((x) < (y)) ? (x) : (y))
    
    void example(unsigned int old_val)
    {
        unsigned int new_val = MIN(old_val, 65535);
    
        if (!new_val) {
        }
    }
    

    This is clearly incorrect.

    $ ~/src/cppcheck/cppcheck --enable=all example.c 
    Checking example.c ...
    example.c:7:6: style: Condition '!new_val' is always true [knownConditionTrueFalse]
     if (!new_val) {
         ^
    example.c:5:25: note: Assignment 'new_val=((old_val)<65535)?old_val:65535', assigned value is less than 1
     unsigned int new_val = MIN(old_val, 65535);
                            ^
    example.c:7:6: note: Condition '!new_val' is always true
     if (!new_val) {
         ^
    example.c:3:0: style: The function 'example' is never used. [unusedFunction]
    void example(unsigned int old_val)
    ^
    Active checkers: 106/565
    
     
  • Andrew C Aitchison

    This code passes with v2.9, but not v2.10 --enable=style

    cppcheck-2.10 --enable=style TF.cpp
    Checking TF.cpp ...
    TF.cpp:7:13: style: Condition '!new_val' is always false [knownConditionTrueFalse]
    if (!new_val) {
    ^
    TF.cpp:5:32: note: Assignment 'new_val=((old_val)<65535)?old_val:65535', assigned value is less than 1
    unsigned int new_val = MIN(old_val, 65535);
    ^
    TF.cpp:7:13: note: Condition '!new_val' is always false
    if (!new_val) {
    ^

     
  • CHR

    CHR - 2023-09-29
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.