]> git.ipfire.org Git - thirdparty/gcc.git/commit
c, ubsan: Instrument even shortened divisions [PR109151]
authorJakub Jelinek <jakub@redhat.com>
Fri, 17 Mar 2023 15:10:14 +0000 (16:10 +0100)
committerJakub Jelinek <jakub@redhat.com>
Sun, 19 Mar 2023 05:28:58 +0000 (06:28 +0100)
commitffb22189be90216e6af386aaef8b76d2dcd05b84
treee7db46c3f7e1ccc3396268d90f4a1c786f10ab30
parentd1b61495f60141dd9c8cad4a699d5adcecc56b85
c, ubsan: Instrument even shortened divisions [PR109151]

On the following testcase, the C FE decides to shorten the division because
it has a guarantee that INT_MIN / -1 division won't be encountered, the
first operand is widened from narrower unsigned and/or the second operand is
a constant other than all ones (in this case both are true).
The problem is that the narrower type in this case is _Bool and
ubsan_instrument_division only instruments it if op0's type is INTEGER_TYPE
or REAL_TYPE.  Strangely this doesn't happen in C++ FE.
Anyway, we only shorten divisions if the INT_MIN / -1 case is impossible,
so I think we should be fine even with -fstrict-enums in C++ in case it
shortened to ENUMERAL_TYPEs.

The following patch just instruments those on the ubsan_instrument_division
side.  Perhaps only the first hunk and testcase might be needed because
we shouldn't shorten if the other case could be triggered.

2023-03-17  Jakub Jelinek  <jakub@redhat.com>

PR c/109151
* c-ubsan.cc (ubsan_instrument_division): Handle all scalar integral
types rather than just INTEGER_TYPE.

* c-c++-common/ubsan/div-by-zero-8.c: New test.

(cherry picked from commit 103d423f6ce72ccb03d55b7b1dfa2dabd5854371)
gcc/c-family/c-ubsan.cc
gcc/testsuite/c-c++-common/ubsan/div-by-zero-8.c [new file with mode: 0644]