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 :-)
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11591
Log in to post a comment.
Not sure if this exact example's been posted before...
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
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11591