Menu

false positive - knownConditionTrueFalse (perhaps due to uint64)

2020-10-03
2021-01-29
  • Henrik Holst

    Henrik Holst - 2020-10-03
    static inline int num_digits (const uint64_t value)
    {
        if (value < __UINT64_C(100000)) {
            if (value < __UINT64_C(10)) return 1;
            if (value < __UINT64_C(100)) return 2;
            if (value < __UINT64_C(1000)) return 3;
            if (value < __UINT64_C(10000)) return 4;
            return 5;
        }
    
        if (value < __UINT64_C(10000000000)) {
            if (value < __UINT64_C(1000000)) return 6;
            if (value < __UINT64_C(10000000)) return 7;
            if (value < __UINT64_C(100000000)) return 8;
            if (value < __UINT64_C(1000000000)) return 9;
            return 10;
        }
    
        if (value < __UINT64_C(1000000000000000)) {
            if (value < __UINT64_C(100000000000)) return 11;
            if (value < __UINT64_C(1000000000000)) return 12;
            if (value < __UINT64_C(10000000000000)) return 13;
            if (value < __UINT64_C(100000000000000)) return 14;
            return 15;
        }
    
        if (value < __UINT64_C(10000000000000000)) return 16;
        if (value < __UINT64_C(100000000000000000)) return 17; /* line 181 */
        if (value < __UINT64_C(1000000000000000000)) return 18;
        if (value < __UINT64_C(10000000000000000000)) return 19;
    
        return 20;
    }
    

    The above code produces this cppcheck error (tested with 2.2):

    src/decode.c:183:12: style: Condition 'value<10000000000000000000ULL' is always false [knownConditionTrueFalse]
     if (value < __UINT64_C(10000000000000000000)) return 19;
               ^
    src/decode.c:181:12: note: Assuming that condition 'value<100000000000000000ULL' is not redundant
     if (value < __UINT64_C(100000000000000000)) return 17;
               ^
    src/decode.c:182:12: note: Assuming condition is false
     if (value < __UINT64_C(1000000000000000000)) return 18;
               ^
    src/decode.c:183:12: note: Condition 'value<10000000000000000000ULL' is always false
     if (value < __UINT64_C(10000000000000000000)) return 19;
    

    Since cppcheck doesn't complain about the other lines I'm guessing that it has something to do with the large numbers, aka as if cppcheck doesn't handle them as 64-bit unsigned.

     
  • CHR

    CHR - 2021-01-25

    This doesn't reproduce with 2.3 in C mode, but maybe the macro UINT64_C should be defined somewhere?

     
  • Henrik Holst

    Henrik Holst - 2021-01-25

    Ah the warning disappears with -I /usr/include/ , assumed for some reason that cppcheck would check /usr/include by default.

     
  • CHR

    CHR - 2021-01-25

    With this modified code, I get the warnings shown above. Note that 10000000000000000000 is still less than UINT64_MAX = 18446744073709551615.

    static inline int num_digits (const uint64_t value)
    {
        if (value < 100000ull) {
            if (value < 10ull) return 1;
            if (value < 100ull) return 2;
            if (value < 1000ull) return 3;
            if (value < 10000ull) return 4;
            return 5;
        }
    
        if (value < 10000000000ull) {
            if (value < 1000000ull) return 6;
            if (value < 10000000ull) return 7;
            if (value < 100000000ull) return 8;
            if (value < 1000000000ull) return 9;
            return 10;
        }
    
        if (value < 1000000000000000ull) {
            if (value < 100000000000ull) return 11;
            if (value < 1000000000000ull) return 12;
            if (value < 10000000000000ull) return 13;
            if (value < 100000000000000ull) return 14;
            return 15;
        }
    
        if (value < 10000000000000000ull) return 16;
        if (value < 100000000000000000ull) return 17; /* line 181 */
        if (value < 1000000000000000000ull) return 18;
        if (value < 10000000000000000000ull) return 19;
    
        return 20;
    }
    
     
  • CHR

    CHR - 2021-01-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.