public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] ICE with ~INT_MAX in fold-const.c (PR sanitizer/80875)
@ 2017-05-25 16:52 Marek Polacek
  2017-05-26  9:06 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2017-05-25 16:52 UTC (permalink / raw)
  To: GCC Patches

We ICE on this testcase in
 9812           /* Transform x * -C into -x * C if x is easily negatable.  */
 9813           if (TREE_CODE (op1) == INTEGER_CST
 9814               && tree_int_cst_sgn (op1) == -1
 9815               && negate_expr_p (op0)
 9816               && (tem = negate_expr (op1)) != op1
 9817               && ! TREE_OVERFLOW (tem))
because fold_negate_expr returns NULL_TREE for INT_MIN, so negate_expr just
wrapped in into a NEGATE_EXPR, creating NEGATE_EXPR <INT_MIN>.  TREE_OVERFLOW
crashes on that.  I thought it made sense to check whether we can negate OP1
first, as done in the patch below.

Bootstrapped/regtested on x86_64-linux, ok for trunk and 7?

2017-05-25  Marek Polacek  <polacek@redhat.com>

	PR sanitizer/80875
	* fold-const.c (fold_binary_loc) <case MULT_EXPR>: Check if OP1
	can be negated.

	* c-c++-common/ubsan/pr80875.c: New test.

diff --git gcc/fold-const.c gcc/fold-const.c
index efc0b10..911ae36 100644
--- gcc/fold-const.c
+++ gcc/fold-const.c
@@ -9813,6 +9813,7 @@ fold_binary_loc (location_t loc,
 	  if (TREE_CODE (op1) == INTEGER_CST
 	      && tree_int_cst_sgn (op1) == -1
 	      && negate_expr_p (op0)
+	      && negate_expr_p (op1)
 	      && (tem = negate_expr (op1)) != op1
 	      && ! TREE_OVERFLOW (tem))
 	    return fold_build2_loc (loc, MULT_EXPR, type,
diff --git gcc/testsuite/c-c++-common/ubsan/pr80875.c gcc/testsuite/c-c++-common/ubsan/pr80875.c
index e69de29..e679452 100644
--- gcc/testsuite/c-c++-common/ubsan/pr80875.c
+++ gcc/testsuite/c-c++-common/ubsan/pr80875.c
@@ -0,0 +1,9 @@
+/* PR sanitizer/80875 */
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=undefined" } */
+
+int
+foo (void)
+{
+  return ~__INT_MAX__ * (0 / 0); /* { dg-warning "division by zero" } */
+}

	Marek

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

* Re: [PATCH] ICE with ~INT_MAX in fold-const.c (PR sanitizer/80875)
  2017-05-25 16:52 [PATCH] ICE with ~INT_MAX in fold-const.c (PR sanitizer/80875) Marek Polacek
@ 2017-05-26  9:06 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2017-05-26  9:06 UTC (permalink / raw)
  To: Marek Polacek; +Cc: GCC Patches

On Thu, May 25, 2017 at 6:33 PM, Marek Polacek <polacek@redhat.com> wrote:
> We ICE on this testcase in
>  9812           /* Transform x * -C into -x * C if x is easily negatable.  */
>  9813           if (TREE_CODE (op1) == INTEGER_CST
>  9814               && tree_int_cst_sgn (op1) == -1
>  9815               && negate_expr_p (op0)
>  9816               && (tem = negate_expr (op1)) != op1
>  9817               && ! TREE_OVERFLOW (tem))
> because fold_negate_expr returns NULL_TREE for INT_MIN, so negate_expr just
> wrapped in into a NEGATE_EXPR, creating NEGATE_EXPR <INT_MIN>.  TREE_OVERFLOW
> crashes on that.  I thought it made sense to check whether we can negate OP1
> first, as done in the patch below.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk and 7?

Ok.

> 2017-05-25  Marek Polacek  <polacek@redhat.com>
>
>         PR sanitizer/80875
>         * fold-const.c (fold_binary_loc) <case MULT_EXPR>: Check if OP1
>         can be negated.
>
>         * c-c++-common/ubsan/pr80875.c: New test.
>
> diff --git gcc/fold-const.c gcc/fold-const.c
> index efc0b10..911ae36 100644
> --- gcc/fold-const.c
> +++ gcc/fold-const.c
> @@ -9813,6 +9813,7 @@ fold_binary_loc (location_t loc,
>           if (TREE_CODE (op1) == INTEGER_CST
>               && tree_int_cst_sgn (op1) == -1
>               && negate_expr_p (op0)
> +             && negate_expr_p (op1)
>               && (tem = negate_expr (op1)) != op1
>               && ! TREE_OVERFLOW (tem))
>             return fold_build2_loc (loc, MULT_EXPR, type,
> diff --git gcc/testsuite/c-c++-common/ubsan/pr80875.c gcc/testsuite/c-c++-common/ubsan/pr80875.c
> index e69de29..e679452 100644
> --- gcc/testsuite/c-c++-common/ubsan/pr80875.c
> +++ gcc/testsuite/c-c++-common/ubsan/pr80875.c
> @@ -0,0 +1,9 @@
> +/* PR sanitizer/80875 */
> +/* { dg-do compile } */
> +/* { dg-options "-fsanitize=undefined" } */
> +
> +int
> +foo (void)
> +{
> +  return ~__INT_MAX__ * (0 / 0); /* { dg-warning "division by zero" } */
> +}
>
>         Marek

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

end of thread, other threads:[~2017-05-26  8:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-25 16:52 [PATCH] ICE with ~INT_MAX in fold-const.c (PR sanitizer/80875) Marek Polacek
2017-05-26  9:06 ` Richard Biener

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).