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

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



On 2014-04-12 20:32:33 -0700, Russ Allbery wrote:
> I enabled -fstrict-overflow -Wstrict-overflow=5 -Werror in my standard
[...]

GCC does silly things with -Wstrict-overflow=5.

For instance, consider the following code:

int foo (int d)
{
  int m;
  m = d * 64;
  return m;
}

With "gcc -O2 -fstrict-overflow -Wstrict-overflow=5", everything
is fine. But if return value is replaced by "m >= 0", giving the
following code:

int foo (int d)
{
  int m;
  m = d * 64;
  return m >= 0;
}

I get:

tst.c: In function ‘foo’:
tst.c:5:12: warning: assuming signed overflow does not occur when eliminating multiplication in comparison with zero [-Wstrict-overflow]
   return m >= 0;
            ^

while the cause of a potential bug would be the same. For consistency,
GCC should have warned for the first code too.

This affects the compilation of the MPFR trunk, which has similar
code... excepts that MPFR also has an overflow check, which isn't
used by GCC, like that:

#include <limits.h>

#define C 64

int foo (int d)
{
  int m;
  if (d <= 0 || d > INT_MAX / C)
    return 0;
  m = d * C;
  return m >= 0;
}

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


Reply to: