Menu

Underflow/overflow

2023-02-28
2023-03-01
  • Neil Matthews

    Neil Matthews - 2023-02-28

    Not sure if this exact example's been posted before...

    #include <stdio.h>
    
    int main(int argc, char *argv)
    {
        int array[1] = {0};
        unsigned char i = -1;
        i++;
        printf("%d\n", array[i]);
    }
    

    The array index i is an unsigned char which is underflowed by assigning -1 to it, i becomes 255.
    Then the index i is overflowed back to 0.

    cppcheck seems to treat i as 256 rather than 0.

    And no, I won't defend the code :-)

     

    Last edit: Neil Matthews 2023-02-28
  • CHR

    CHR - 2023-03-01

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

     

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.