aboutsummaryrefslogtreecommitdiff
path: root/gcc/fold-const.cc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2023-01-04 10:52:49 +0100
committerJakub Jelinek <jakub@redhat.com>2023-01-04 10:52:49 +0100
commit8692b15ae7c05e3224f285069e070c009d9f6efe (patch)
tree72c7c7360e12e17add721f5b3d82c54a872151ff /gcc/fold-const.cc
parent44baa34157cf81306be23eacece751aa020985d4 (diff)
downloadgcc-8692b15ae7c05e3224f285069e070c009d9f6efe.zip
gcc-8692b15ae7c05e3224f285069e070c009d9f6efe.tar.gz
gcc-8692b15ae7c05e3224f285069e070c009d9f6efe.tar.bz2
ubsan: Avoid narrowing of multiply for -fsanitize=signed-integer-overflow [PR108256]
We shouldn't narrow multiplications originally done in signed types, because the original multiplication might overflow but the narrowed one will be done in unsigned arithmetics and will never overflow. 2023-01-04 Jakub Jelinek <jakub@redhat.com> PR sanitizer/108256 * convert.cc (do_narrow): Punt for MULT_EXPR if original type doesn't wrap around and -fsanitize=signed-integer-overflow is on. * fold-const.cc (fold_unary_loc) <CASE_CONVERT>: Likewise. * c-c++-common/ubsan/pr108256.c: New test.
Diffstat (limited to 'gcc/fold-const.cc')
-rw-r--r--gcc/fold-const.cc4
1 files changed, 3 insertions, 1 deletions
diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 9ab76fb..9aaea71 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -9574,7 +9574,9 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
if (INTEGRAL_TYPE_P (type)
&& TREE_CODE (op0) == MULT_EXPR
&& INTEGRAL_TYPE_P (TREE_TYPE (op0))
- && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
+ && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
+ && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
+ || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
{
/* Be careful not to introduce new overflows. */
tree mult_type;