[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Having fun with the following C code (UB)



On 14/04/14 14:30, Vincent Lefevre wrote:
> On 2014-04-14 14:14:14 +0200, Raphael Geissert wrote:
>> No, there is no optimisation in that case, so there is no warning. It only 
>> warns when it uses the knowledge that "(signed) integer overflow isn't 
>> possible" to optimise away some redundant code.
> 
> But what I mean is that it's pointless to emit such a warning when
> the effect of the potential integer overflow is already visible,
> for instance in printf below:
> 
>   m = d * C;
>   printf ("%d\n", m);
>   return m >= 0;
> 
> If there was an integer overflow, you will get an incorrect value
> output by the printf. This means that it is very likely to be a false
> positive. So, one doesn't want the warning.

The purpose of this gcc warning isn't to warn you that overflow might
happen, but to warn you when gcc's optimisations have broken any two's
complement overflow behaviour that you might have expected. Thus if you have
written code that assumes "normal" two's complement overflow you get a
warning when it has been broken by assumptions made by the optimiser. In
other cases you get "normal" overflow so there is no need for this warning.

Roger


Reply to: