This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[3/4] Avoid undefined operation in overflow check
- From: Richard Sandiford <rdsandiford at googlemail dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Tue, 19 Nov 2013 12:06:06 +0000
- Subject: [3/4] Avoid undefined operation in overflow check
- Authentication-results: sourceware.org; auth=none
- References: <871u2cpq4u dot fsf at talisman dot default>
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.
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;