Gcc silently transforms a finite loop to an infinite one

David Brown david@westcontrol.com
Tue Nov 29 18:52:00 GMT 2011


On 29/11/2011 15:25, Ian Lance Taylor wrote:
> Nadezhda Ivanоvna Vyukova<niva@niisi.msk.ru>  writes:
>
>> I am involved in support of customers who use GCC.
>> Recently a customer has complaint that gcc-4.1.2
>> generates an infinite loop for the following program:
>>
>>    #include<stdio.h>
>>    extern void f (int);
>>    int main ()
>>    {
>>    char e = 0;
>>       do
>>       {
>>         printf ("--- e = %i\n", e);
>>       }
>>       while (++e);
>>    }
>>
>> When compiled with -O2 or higher the program prints
>> 0, 1, ..., 128, ... 1000, ... etc.
>> (Similar behavior is exposed by gcc-4.6.2).
>>
>> I've explained the customer that by default char is treated
>> as signed char on our platform and therefore this program
>> does not conform ISO C90, as it causes the integer overflow
>> (undefined behavior). But he was not satisfied.
>> He argued that the program compiled with gcc-3.4.6 behaves
>> "correctly" and now the compiler silently produces an
>> incomprehensible code.
>
> Tell your customer to use the -fno-strict-overflow option.
>
>> IMHO it would be better to issue a warning
>> when a finite loop is transformed to an infinite one
>> (as a result of -ftree-vrp).
>
> Tell your customer to use the -Wstrict-overflow option.
>
> This message might have been better directed to gcc-help@gcc.gnu.org.
>
> I would have no objection to adding a default warning when a finite loop
> is converted to an infinite loop.  It's harder than it sounds, though.
> It's not like gcc is looking a loop and deciding to make it infinite.
> It's looking at a branch instruction and deciding that the condition can
> never be true.
>
> Ian
>

A better method is perhaps to get users to enable warning flags by 
default - but that's also harder than it sounds!

I've always thought that gcc should have two new warning flags that are 
enabled by default - one being the "Warning - no warning flags given on 
command line" flag, and the other being the "Warning - compiler crippled 
by lack of -O flag" flag :-)




More information about the Gcc mailing list