BUG: assuming signed overflow does not occur when simplifying conditional to constant

Bruce Korb bruce.korb@gmail.com
Sat Dec 29 10:38:00 GMT 2012


I wrote a loop that figures out how many items are in a list,
counts down from that count to -1, changes direction and counts
up from 0 to the limit, a la:


    inc  = -1;
    int idx = 0;
    while (opts->papzHomeList[idx+1] != NULL)
        idx++;
    for (;;) {
        if (idx < 0) {  <<<=== line 1025
            inc = 1;
            idx = 0;
        }
        char const * path = opts->papzHomeList[ idx ];
        if (path == NULL)
            break;
        idx += inc;
        //  do a bunch of stuff
    }

../../autoopts/configfile.c: In function 'intern_file_load':
../../autoopts/configfile.c:1025:12: error: assuming signed overflow does not occur \
    when simplifying conditional to constant [-Werror=strict-overflow]

I do not think GCC should be "simplifying" the expression "idx < 0" to a constant.
The number of entries in "papzHomeList" is known to be less than INT_MAX (64K actually),
but not by the code in question.  My guess is that some code somewhere presumes
that "idx" never gets decremented.  Not true.



More information about the Gcc mailing list