I'm getting this weird behaviour from an executable compiled with different versions of gcc
, all emit the SIGFPE
signal and the best part is that I have no floating point of any kind in my code; if someone could shed some light on this ... I literally don't know where to start to debug this, it's so weird and this bug is triggered by all the gcc installations that I have from 4.9
to 6.0
.
Here is a snippet that reproduces the problem
// Floating point exception - SIGFPE
#include <stdio.h>
typedef unsigned int T;
int main()
{
#define N 256
for (T i = 0; i < N; ++i)
{
i += (i % i);
printf("%u\t", i);
}
}
// bug uncovered with
// gcc version 4.9.2 (Debian 4.9.2-10)
// gcc version 5.1.0 (GCC)
// gcc version 6.0.0 20150517 (experimental) (GCC)
// using -std=c11 or -std=c99
The purpose of this code is to reproduce the problem, I know that the logic of it doesn't really make too much sense ( the modulo part ) but clang
passes the test, no version of gcc
does the same and I would like to know why if there is a technical explanation for this kind of behaviour .
After running the code, this was under cygwin, gdb dumped the trace.
The clue is in the operation
i += (i % i)
when the loop is initial value of 0, of course, divide by zero error.
Have you tried to catch the signal?
Look at the C11 standard on Page 265, SIGFPE - an erroneous arithmetic operation, such as zero divide or an operation resulting in overflow
It is not a compiler bug, that is implementation defined.