GCC and division by 0 under sanitizers

Segher Boessenkool segher@kernel.crashing.org
Sun Mar 29 21:43:03 GMT 2020


On Sun, Mar 29, 2020 at 05:01:39PM -0400, Jeffrey Walton via Gcc-help wrote:
> I'm having trouble with floating point arithmetic and division by 0.
> Undefined behavior sanitizer flags a division by 0 is a runtime error.

int f(int x) { return x / 0; }
float g(float x) { return x / 0; }

This actually warns during build for both functions:
$ gcc -Wall -W -O2 -fsanitize=undefined dz.c
dz.c: In function 'f':
dz.c:1:25: warning: division by zero [-Wdiv-by-zero]
    1 | int f(int x) { return x / 0; }
      |                         ^
dz.c: In function 'g':
dz.c:2:29: warning: division by zero [-Wdiv-by-zero]
    2 | float g(float x) { return x / 0; }
      |                             ^

but only f does anything with ubsan, as it should.  (Please open a PR
for the warning:
'-Wno-div-by-zero'
     Do not warn about compile-time integer division by zero.
     Floating-point division by zero is not warned about, as it can be a
     legitimate way of obtaining infinities and NaNs.
We clearly have a floating point division here.)

What do you do to see ubsan misfire?


Segher



More information about the Gcc-help mailing list