If we have an unsigned type, we cannot do this since it will change
the result if the original computation overflowed. */
if (TYPE_OVERFLOW_UNDEFINED (ctype)
+ && !TYPE_OVERFLOW_SANITIZED (ctype)
&& ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
|| (tcode == MULT_EXPR
&& code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
TYPE_SIGN (type)))
{
- if (TYPE_OVERFLOW_UNDEFINED (ctype))
- *strict_overflow_p = true;
+ *strict_overflow_p = true;
return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
fold_convert (ctype,
const_binop (TRUNC_DIV_EXPR,
else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
TYPE_SIGN (type)))
{
- if (TYPE_OVERFLOW_UNDEFINED (ctype))
- *strict_overflow_p = true;
+ *strict_overflow_p = true;
return fold_build2 (code, ctype, fold_convert (ctype, op0),
fold_convert (ctype,
const_binop (TRUNC_DIV_EXPR,
--- /dev/null
+/* { dg-do run { target int32 } } */
+/* { dg-shouldfail "ubsan" } */
+/* With optimization we constant fold and diagnose the overflow and do
+ not sanitize anything. */
+/* { dg-skip-if "" { *-*-* } { "*" } { ! "-O0" } } */
+/* { dg-options "-fsanitize=undefined -fno-sanitize-recover=undefined" } */
+
+int a;
+const int b = 44514;
+int *c = &a;
+
+int main ()
+{
+ *c = 65526 * b / 6;
+ return 0;
+}
+
+/* { dg-output "signed integer overflow: 44514 \\* 65526 cannot be represented in type 'int'" } */