Traps for signed arithmetic overflow
Helmut Eller
eller.helmut@gmail.com
Fri Nov 23 21:27:00 GMT 2018
On Fri, Nov 23 2018, Marc Glisse wrote:
> On Fri, 23 Nov 2018, Helmut Eller wrote:
>
>> when compiling this example with gcc -O2 -ftrapv:
>> [...]
> Try with "-fsanitize=undefined
> -fsanitize-undefined-trap-on-error". -ftrapv is quite broken and not
> really maintained. I think fixing it would mean making it an alias for
> the sanitizer version.
Thanks for the quick reply. That does indeed produce exactly the code I
want.
It doesn't seem so detect overflow for short or char types, though:
typedef signed short si;
si foo (si x, si y) { return x + y; }
si bar (si x, si y) {
si z;
if (__builtin_add_overflow (x, y, &z))
__builtin_trap ();
return z;
}
compiled with either -O2 -fsanitize=signed-integer-overflow
-fsanitize-undefined-trap-on-error or -ftrapv produces:
foo:
leal (%rsi,%rdi), %eax
ret
bar:
movswl %di, %eax
movswl %si, %edi
addw %di, %ax
jo .L8
rep ret
.L8:
ud2
For __int128 it seems to be detected again, but the code seems very
long-winded.
Helmut
More information about the Gcc-help
mailing list