This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [3/4] Avoid undefined operation in overflow check
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>, Richard Sandiford <rdsandiford at googlemail dot com>
- Date: Wed, 20 Nov 2013 10:46:34 +0100
- Subject: Re: [3/4] Avoid undefined operation in overflow check
- Authentication-results: sourceware.org; auth=none
- References: <871u2cpq4u dot fsf at talisman dot default> <87li0koatt dot fsf at talisman dot default>
On Tue, Nov 19, 2013 at 1:06 PM, Richard Sandiford
<rdsandiford@googlemail.com> wrote:
> This is a case where tree_to_shwi can be used instead of TREE_INT_CST_LOW.
> I separated it out because it was using a signed "x * y / y == x" to check
> whether "x * y" overflows a HWI, which relies on undefined behaviour.
Please CSE tree_to_shwi (size) (yeah, you may use an ugly
&& (hsz = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT
Ok with that change.
Thanks,
Richard.
> Thanks,
> Richard
>
>
> gcc/
> * tree-ssa-alias.c (ao_ref_init_from_ptr_and_size): Avoid signed
> overflow. Use tree_to_shwi.
>
> Index: gcc/tree-ssa-alias.c
> ===================================================================
> --- gcc/tree-ssa-alias.c 2013-11-19 10:53:54.965643984 +0000
> +++ gcc/tree-ssa-alias.c 2013-11-19 11:08:51.882992035 +0000
> @@ -615,9 +615,8 @@ ao_ref_init_from_ptr_and_size (ao_ref *r
> ref->offset += extra_offset;
> if (size
> && tree_fits_shwi_p (size)
> - && TREE_INT_CST_LOW (size) * BITS_PER_UNIT / BITS_PER_UNIT
> - == TREE_INT_CST_LOW (size))
> - ref->max_size = ref->size = TREE_INT_CST_LOW (size) * BITS_PER_UNIT;
> + && tree_to_shwi (size) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
> + ref->max_size = ref->size = tree_to_shwi (size) * BITS_PER_UNIT;
> else
> ref->max_size = ref->size = -1;
> ref->ref_alias_set = 0;