Menu

FP truncLongCastAssignment

2025-12-05
2025-12-05
  • Marcin Pytel

    Marcin Pytel - 2025-12-05

    I'm using cppcheck 2.18.0

    Here is code:

    // scanf and printf are just to get rid of unused and uninitialized variable
    int main()
    {
            uint32_t a;
            scanf("%u", &a);
            uint32_t i = a, j = a;
            uint64_t k = i * j;
            printf("%lu", k);
    }
    

    When running cppcheck --enable=style I have error:

    style: int result is assigned to long variable. If the variable is long to avoid loss of information, then you have loss of information. [truncLongCastAssignment]
    
    uint64_t k = i * j;
    

    There's no issue when I remove the scanf and assign some number to the a variable e.g. uint32_t a = 5;

     
  • CHR

    CHR - 2025-12-05

    That seems to be a true positive. The multiplication is done using 32 bits, and the result is assigned to a 64 bit value. In case of a = 5, cppcheck knows that no overflow can occur.

     

Log in to post a comment.