https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64006
Bug ID: 64006
Summary: __builtin_mul_overflow fails to signal overflow
Product: gcc
Version: 5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: jtaylor.debian at googlemail dot com
following code does not print overflow while the result does clearly overflow:
$ cat test.c
#include <stdio.h>
int __attribute__((noinline)) wrapper(long a, long b, long * r)
{
return __builtin_mul_overflow(a, b, r);
}
long __attribute__((noinline)) test(long *dims, int nd)
{
int i;
long size = 1;
for (i = 0; i < nd; i++) {
long dim = dims[i];
if (__builtin_mul_overflow(size, dim, &size)) {
//if (wrapper(size, dim, &size)) {
puts("overflow");
}
}
return size;
}
int main(int argc, char * argv[])
{
long r;
long dim[7] = {975,975,975,975,975,975,975};
r = test(dim, 7);
printf("%ld\n",r);
}
$ gcc-5.0 -O2 test.c
$ ./a.out
7488110182826036655
if one uses the commented wrapper function version it correctly prints
overflow.
$ gcc-5.0 --version
gcc (GCC) 5.0.0 20141119 (experimental)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc-5.0 -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/media/jtaylor/45d3164a-104c-4489-9021-091002eb6730/data/local-5.0/bin/../libexec/gcc/x86_64-unknown-linux-gnu/5.0.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with:
/media/jtaylor/45d3164a-104c-4489-9021-091002eb6730/data/gcc/configure
--disable-werror --enable-languages=c,c++,fortran --enable-tls
--prefix=/media/jtaylor/45d3164a-104c-4489-9021-091002eb6730/data//gcc/local-5.0
--with-gmp=/usr --with-mpfr=/usr --with-mpc=/usr --with-cloog=/usr
--with-ppl=/usr --with-isl=/usr --disable-bootstrap
Thread model: posix
gcc version 5.0.0 20141119 (experimental) (GCC)