diff mbox series

Fix unchecked use of tree_to_uhwi in tree-ssa-strlen.c

Message ID mpt36fz730h.fsf@arm.com
State New
Headers show
Series Fix unchecked use of tree_to_uhwi in tree-ssa-strlen.c | expand

Commit Message

Richard Sandiford Oct. 11, 2019, 2:47 p.m. UTC
r273783 introduced an unchecked use of tree_to_uhwi.  This is
tested by the SVE ACLE patches, but could potentially trigger
in non-SVE cases too.

Tested on aarch64-linux-gnu and x86_64-linux-gnu.  OK to install?

Richard


2019-10-11  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
	* tree-ssa-strlen.c (count_nonzero_bytes): Check tree_fits_uhwi_p
	before using tree_to_uhwi.

Comments

Martin Sebor Oct. 11, 2019, 3:14 p.m. UTC | #1
On 10/11/19 8:47 AM, Richard Sandiford wrote:
> r273783 introduced an unchecked use of tree_to_uhwi.  This is
> tested by the SVE ACLE patches, but could potentially trigger
> in non-SVE cases too.
> 
> Tested on aarch64-linux-gnu and x86_64-linux-gnu.  OK to install?

I wasn't able to come up with a C test case that triggers the bug
but the fix seems obviously safe and correct to me.  Thanks for
handling it!

Martin

> 
> Richard
> 
> 
> 2019-10-11  Richard Sandiford  <richard.sandiford@arm.com>
> 
> gcc/
> 	* tree-ssa-strlen.c (count_nonzero_bytes): Check tree_fits_uhwi_p
> 	before using tree_to_uhwi.
> 
> Index: gcc/tree-ssa-strlen.c
> ===================================================================
> --- gcc/tree-ssa-strlen.c	2019-10-11 15:43:51.127514545 +0100
> +++ gcc/tree-ssa-strlen.c	2019-10-11 15:46:11.718524445 +0100
> @@ -4026,10 +4026,10 @@ count_nonzero_bytes (tree exp, unsigned
>   
>         /* The size of the MEM_REF access determines the number of bytes.  */
>         tree type = TREE_TYPE (exp);
> -      if (tree typesize = TYPE_SIZE_UNIT (type))
> -	nbytes = tree_to_uhwi (typesize);
> -      else
> +      tree typesize = TYPE_SIZE_UNIT (type);
> +      if (!typesize || !tree_fits_uhwi_p (typesize))
>   	return false;
> +      nbytes = tree_to_uhwi (typesize);
>   
>         /* Handle MEM_REF = SSA_NAME types of assignments.  */
>         return count_nonzero_bytes (arg, offset, nbytes, lenrange, nulterm,
>
Richard Biener Oct. 14, 2019, 11:43 a.m. UTC | #2
On Fri, Oct 11, 2019 at 4:47 PM Richard Sandiford
<richard.sandiford@arm.com> wrote:
>
> r273783 introduced an unchecked use of tree_to_uhwi.  This is
> tested by the SVE ACLE patches, but could potentially trigger
> in non-SVE cases too.
>
> Tested on aarch64-linux-gnu and x86_64-linux-gnu.  OK to install?

OK.

Richard.

> Richard
>
>
> 2019-10-11  Richard Sandiford  <richard.sandiford@arm.com>
>
> gcc/
>         * tree-ssa-strlen.c (count_nonzero_bytes): Check tree_fits_uhwi_p
>         before using tree_to_uhwi.
>
> Index: gcc/tree-ssa-strlen.c
> ===================================================================
> --- gcc/tree-ssa-strlen.c       2019-10-11 15:43:51.127514545 +0100
> +++ gcc/tree-ssa-strlen.c       2019-10-11 15:46:11.718524445 +0100
> @@ -4026,10 +4026,10 @@ count_nonzero_bytes (tree exp, unsigned
>
>        /* The size of the MEM_REF access determines the number of bytes.  */
>        tree type = TREE_TYPE (exp);
> -      if (tree typesize = TYPE_SIZE_UNIT (type))
> -       nbytes = tree_to_uhwi (typesize);
> -      else
> +      tree typesize = TYPE_SIZE_UNIT (type);
> +      if (!typesize || !tree_fits_uhwi_p (typesize))
>         return false;
> +      nbytes = tree_to_uhwi (typesize);
>
>        /* Handle MEM_REF = SSA_NAME types of assignments.  */
>        return count_nonzero_bytes (arg, offset, nbytes, lenrange, nulterm,
diff mbox series

Patch

Index: gcc/tree-ssa-strlen.c
===================================================================
--- gcc/tree-ssa-strlen.c	2019-10-11 15:43:51.127514545 +0100
+++ gcc/tree-ssa-strlen.c	2019-10-11 15:46:11.718524445 +0100
@@ -4026,10 +4026,10 @@  count_nonzero_bytes (tree exp, unsigned
 
       /* The size of the MEM_REF access determines the number of bytes.  */
       tree type = TREE_TYPE (exp);
-      if (tree typesize = TYPE_SIZE_UNIT (type))
-	nbytes = tree_to_uhwi (typesize);
-      else
+      tree typesize = TYPE_SIZE_UNIT (type);
+      if (!typesize || !tree_fits_uhwi_p (typesize))
 	return false;
+      nbytes = tree_to_uhwi (typesize);
 
       /* Handle MEM_REF = SSA_NAME types of assignments.  */
       return count_nonzero_bytes (arg, offset, nbytes, lenrange, nulterm,