Fix PR81782

Message ID alpine.LSU.2.20.1712081543220.12252@zhemvz.fhfr.qr
State New
Headers show
Series
  • Fix PR81782
Related show

Commit Message

Richard Biener Dec. 8, 2017, 2:44 p.m.
The following fixes spurious uninit warnings for zero-sized arrays.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.

Richard.

2017-12-08  Richard Biener  <rguenther@suse.de>

	PR middle-end/81782
	* tree-ssa-uninit.c (warn_uninitialized_vars): Properly
	handle accesses outside of zero-sized vars.

	* gcc.dg/uninit-pr81782.c: New testcase.

Comments

Jakub Jelinek Dec. 8, 2017, 2:58 p.m. | #1
On Fri, Dec 08, 2017 at 03:44:03PM +0100, Richard Biener wrote:
> 

> The following fixes spurious uninit warnings for zero-sized arrays.

> 

> Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.

> 

> Richard.

> 

> 2017-12-08  Richard Biener  <rguenther@suse.de>

> 

> 	PR middle-end/81782

> 	* tree-ssa-uninit.c (warn_uninitialized_vars): Properly

> 	handle accesses outside of zero-sized vars.


Is ref.max_size == -1 always guaranteeing no access before offset, only
after it?
What I fear is e.g. ARRAY_REF with non-constant index with base of
MEM_REF with & of a middle of array or something similar?
struct S { int x[64]; } foo;
MEM_REF[&foo, 40][i] with negative i.

Anyway, in this case it is about not printing a warning, so we can give up
even if it is theoretically possible.

> --- gcc/tree-ssa-uninit.c	(revision 255499)

> +++ gcc/tree-ssa-uninit.c	(working copy)

> @@ -296,8 +296,8 @@ warn_uninitialized_vars (bool warn_possi

>  	         variable.  */

>  	      if (DECL_P (base)

>  		  && ref.size != -1

> -		  && ref.max_size == ref.size

> -		  && (ref.offset + ref.size <= 0

> +		  && ((ref.max_size == ref.size

> +		       && ref.offset + ref.size <= 0)

>  		      || (ref.offset >= 0

>  			  && DECL_SIZE (base)

>  			  && TREE_CODE (DECL_SIZE (base)) == INTEGER_CST


	Jakub
Richard Biener Dec. 8, 2017, 3:51 p.m. | #2
On December 8, 2017 3:58:11 PM GMT+01:00, Jakub Jelinek <jakub@redhat.com> wrote:
>On Fri, Dec 08, 2017 at 03:44:03PM +0100, Richard Biener wrote:

>> 

>> The following fixes spurious uninit warnings for zero-sized arrays.

>> 

>> Bootstrapped and tested on x86_64-unknown-linux-gnu, applied.

>> 

>> Richard.

>> 

>> 2017-12-08  Richard Biener  <rguenther@suse.de>

>> 

>> 	PR middle-end/81782

>> 	* tree-ssa-uninit.c (warn_uninitialized_vars): Properly

>> 	handle accesses outside of zero-sized vars.

>

>Is ref.max_size == -1 always guaranteeing no access before offset, only

>after it?


Yes. 

>What I fear is e.g. ARRAY_REF with non-constant index with base of

>MEM_REF with & of a middle of array or something similar?

>struct S { int x[64]; } foo;

>MEM_REF[&foo, 40][i] with negative i.


The array type constraints the access. Invalid accesses invoke undefined behavior. 

>Anyway, in this case it is about not printing a warning, so we can give

>up

>even if it is theoretically possible.

>

>> --- gcc/tree-ssa-uninit.c	(revision 255499)

>> +++ gcc/tree-ssa-uninit.c	(working copy)

>> @@ -296,8 +296,8 @@ warn_uninitialized_vars (bool warn_possi

>>  	         variable.  */

>>  	      if (DECL_P (base)

>>  		  && ref.size != -1

>> -		  && ref.max_size == ref.size

>> -		  && (ref.offset + ref.size <= 0

>> +		  && ((ref.max_size == ref.size

>> +		       && ref.offset + ref.size <= 0)

>>  		      || (ref.offset >= 0

>>  			  && DECL_SIZE (base)

>>  			  && TREE_CODE (DECL_SIZE (base)) == INTEGER_CST

>

>	Jakub

Patch

Index: gcc/tree-ssa-uninit.c
===================================================================
--- gcc/tree-ssa-uninit.c	(revision 255499)
+++ gcc/tree-ssa-uninit.c	(working copy)
@@ -296,8 +296,8 @@  warn_uninitialized_vars (bool warn_possi
 	         variable.  */
 	      if (DECL_P (base)
 		  && ref.size != -1
-		  && ref.max_size == ref.size
-		  && (ref.offset + ref.size <= 0
+		  && ((ref.max_size == ref.size
+		       && ref.offset + ref.size <= 0)
 		      || (ref.offset >= 0
 			  && DECL_SIZE (base)
 			  && TREE_CODE (DECL_SIZE (base)) == INTEGER_CST
Index: gcc/testsuite/gcc.dg/uninit-pr81782.c
===================================================================
--- gcc/testsuite/gcc.dg/uninit-pr81782.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/uninit-pr81782.c	(working copy)
@@ -0,0 +1,14 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wmaybe-uninitialized" } */
+
+int
+foo (void)
+{
+  char empty_array[] = { };
+  int i, ret = 0;
+
+  for (i = 0; i < (int) (sizeof (empty_array) / sizeof (empty_array[0])); i++)
+    ret = empty_array[i]; /* { dg-bogus "uninitialized" } */
+
+  return ret;
+}