public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* GCC and division by 0 under sanitizers
@ 2020-03-29 21:01 Jeffrey Walton
  2020-03-29 21:43 ` Segher Boessenkool
  0 siblings, 1 reply; 2+ messages in thread
From: Jeffrey Walton @ 2020-03-29 21:01 UTC (permalink / raw)
  To: gcc-help

Hi Everyone,

I'm having trouble with floating point arithmetic and division by 0.
Undefined behavior sanitizer flags a division by 0 is a runtime error.
However, IEEE 754 says it is infinity if the operation does not trap.
(Assuming I am looking at the right version of the standard).

So to test IEEE floating point for division by 0:

    void test_floats(void)
    {
        ASSERT(INFINITY == 1.0f / 0.0f);
        ...
    }

It seems like a reasonable test to me. However, we don't really want
UBsan findings during testing either.

I was looking at pragmas to disable UBsan division-by-zero in the
source file, but I don't see one. Confer,
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html and
https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html.

Given users will CFLAGS="-fsanitize=undefined", what is the way to
handle this in a way that avoids asking the user to do something?

Asking the user to RTFM and use something like
-fsanitize=all,no-divide-by-zero or
-fsanitize-recover=float-divide-by-zero is not going to work. If RTFM
was going to work, it would have happened in the last 50 years or so.
So I want to engineer around the user.

Jeff

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: GCC and division by 0 under sanitizers
  2020-03-29 21:01 GCC and division by 0 under sanitizers Jeffrey Walton
@ 2020-03-29 21:43 ` Segher Boessenkool
  0 siblings, 0 replies; 2+ messages in thread
From: Segher Boessenkool @ 2020-03-29 21:43 UTC (permalink / raw)
  To: noloader; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-03-29 21:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-29 21:01 GCC and division by 0 under sanitizers Jeffrey Walton
2020-03-29 21:43 ` Segher Boessenkool

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).