public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/49754] New: Let gcc warn about all uninitialized variables
@ 2011-07-15  6:17 giecrilj at stegny dot 2a.pl
  2011-07-15  8:38 ` [Bug c/49754] " redi at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: giecrilj at stegny dot 2a.pl @ 2011-07-15  6:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

           Summary: Let gcc warn about all uninitialized variables
           Product: gcc
           Version: 4.5.1
               URL: https://bugzilla.novell.com/show_bug.cgi?id=705160#c1
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: giecrilj@stegny.2a.pl


The compiler gcc currently warns about uninitialized scalar variables but not
about compound variables.

== Steps to Reproduce ==
  1. 
{ cat>foo.c<<'/* EOF */' && gcc -Wall -pedantic-errors foo.c; }
struct i { int x; };
struct i foo () { struct i x; return x; }
int bar () { int y; return +y; }
/* EOF */

== Actual Results ==  
foo.c: In function ‘bar’:
foo.c:3:21: warning: ‘y’ is used uninitialized in this function


== Expected Results ==  
foo.c: In function ‘foo’:
foo.c:2:?: warning: ‘x’ is used uninitialized in this function
foo.c: In function ‘bar’:
foo.c:3:21: warning: ‘y’ is used uninitialized in this function


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

* [Bug c/49754] Let gcc warn about all uninitialized variables
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
@ 2011-07-15  8:38 ` redi at gcc dot gnu.org
  2011-07-15  9:36 ` giecrilj at stegny dot 2a.pl
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2011-07-15  8:38 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-07-15 08:36:30 UTC ---
(In reply to comment #0)
> == Expected Results ==  
> foo.c: In function ‘foo’:
> foo.c:2:?: warning: ‘x’ is used uninitialized in this function

It should really warn if 'x.i' is used uninitialized, not 'x'


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

* [Bug c/49754] Let gcc warn about all uninitialized variables
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
  2011-07-15  8:38 ` [Bug c/49754] " redi at gcc dot gnu.org
@ 2011-07-15  9:36 ` giecrilj at stegny dot 2a.pl
  2011-07-15  9:57 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: giecrilj at stegny dot 2a.pl @ 2011-07-15  9:36 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

--- Comment #2 from Christopher Yeleighton <giecrilj at stegny dot 2a.pl> 2011-07-15 09:35:42 UTC ---
(In reply to comment #1)
> (In reply to comment #0)
> > == Expected Results ==  
> > foo.c: In function ‘foo’:
> > foo.c:2:?: warning: ‘x’ is used uninitialized in this function
> 
> It should really warn if 'x.i' is used uninitialized, not 'x'

Since (x) is uninitialized, so is (x.i).  It is impossible to recognize the
problem when (x.i) is eventually accessed by a client of foo:

{
struct i x = foo (); 
printf ("%i", +x.i);
}


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

* [Bug c/49754] Let gcc warn about all uninitialized variables
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
  2011-07-15  8:38 ` [Bug c/49754] " redi at gcc dot gnu.org
  2011-07-15  9:36 ` giecrilj at stegny dot 2a.pl
@ 2011-07-15  9:57 ` redi at gcc dot gnu.org
  2011-07-15 10:16 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2011-07-15  9:57 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-07-15 09:56:49 UTC ---
(In reply to comment #2)
> Since (x) is uninitialized, so is (x.i).

But what if x.i gets initialized, is x still uninitialized?


struct X { int i; };
struct Y { int i; int j; };

int main()
{
  X x;
  x.i = 0;   // is 'x' initialized now?
  Y y;
  y.i = 0;   // is 'y' initialized now?
  y.j = 0;   // is 'y' initialized now?
}


It would be possible to track the initialization of each subobject *and* the
aggregate, but it would be more overhead


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

* [Bug c/49754] Let gcc warn about all uninitialized variables
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
                   ` (2 preceding siblings ...)
  2011-07-15  9:57 ` redi at gcc dot gnu.org
@ 2011-07-15 10:16 ` redi at gcc dot gnu.org
  2011-07-15 11:01 ` giecrilj at stegny dot 2a.pl
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2011-07-15 10:16 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-07-15 10:16:12 UTC ---
Just to be clear - I completely agree that uninit'd warnings need improving,
I'm not objecting to that.  But in my experience (mostly C++) I'd prefer to
have it tracked at the more fine-grained level of sub-objects.


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

* [Bug c/49754] Let gcc warn about all uninitialized variables
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
                   ` (3 preceding siblings ...)
  2011-07-15 10:16 ` redi at gcc dot gnu.org
@ 2011-07-15 11:01 ` giecrilj at stegny dot 2a.pl
  2012-10-04  8:23 ` [Bug middle-end/49754] Wuninitialized does not work with structs/unions/arrays manu at gcc dot gnu.org
  2020-06-23 21:03 ` msebor at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: giecrilj at stegny dot 2a.pl @ 2011-07-15 11:01 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

--- Comment #5 from Christopher Yeleighton <giecrilj at stegny dot 2a.pl> 2011-07-15 11:00:28 UTC ---
(In reply to comment #3)
> (In reply to comment #2)
> > Since (x) is uninitialized, so is (x.i).
> 
> But what if x.i gets initialized, is x still uninitialized?

If (x.i) denotes an object type and the initial value means "x is empty" then x
is initialized.

> 
> struct X { int i; };
> struct Y { int i; int j; };
> 
> int main()
> {
>   X x;
>   x.i = 0;   // is 'x' initialized now?
>   Y y;
>   y.i = 0;   // is 'y' initialized now?
>   y.j = 0;   // is 'y' initialized now?
> }
> 
> 
> It would be possible to track the initialization of each subobject *and* the
> aggregate, but it would be more overhead

I have already bumped into this using arrays, where GCC does emit a warning
although it should not:

int a [02]; 
for (a [0] = 0;;) if (a [0]) printf ("%d", +a [1]); else a [0] = a [1] = 01;


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

* [Bug middle-end/49754] Wuninitialized does not work with structs/unions/arrays
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
                   ` (4 preceding siblings ...)
  2011-07-15 11:01 ` giecrilj at stegny dot 2a.pl
@ 2012-10-04  8:23 ` manu at gcc dot gnu.org
  2020-06-23 21:03 ` msebor at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: manu at gcc dot gnu.org @ 2012-10-04  8:23 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-10-04
                 CC|                            |manu at gcc dot gnu.org
          Component|c                           |middle-end
            Summary|Let gcc warn about all      |Wuninitialized does not
                   |uninitialized variables     |work with
                   |                            |structs/unions/arrays
     Ever Confirmed|0                           |1

--- Comment #6 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2012-10-04 08:23:24 UTC ---
This is a problem of representation. Struct/unions/arrays are represented as
memory operations, and Wuninitialized does not (generally) work on them. It may
warn in some corner cases if the accesses can be converted into scalar
variables, but in general it doesn't work.

I am not even sure if the current middle-end representation can tell whether
the memory is uninitialized and never initialized up to the point of use. That
would be the first thing to implement.


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

* [Bug middle-end/49754] Wuninitialized does not work with structs/unions/arrays
  2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
                   ` (5 preceding siblings ...)
  2012-10-04  8:23 ` [Bug middle-end/49754] Wuninitialized does not work with structs/unions/arrays manu at gcc dot gnu.org
@ 2020-06-23 21:03 ` msebor at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: msebor at gcc dot gnu.org @ 2020-06-23 21:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49754

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=95848
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Martin Sebor <msebor at gcc dot gnu.org> ---
GCC 11 (and prior) diagnose both functions in comment #0, I suspect as a result
of r156156:

pr49754.c: In function ‘foo’:
pr49754.c:2:38: warning: ‘x’ is used uninitialized [-Wuninitialized]
    2 | struct i foo () { struct i x; return x; }
      |                                      ^
pr49754.c:2:28: note: ‘x’ declared here
    2 | struct i foo () { struct i x; return x; }
      |                            ^
pr49754.c: In function ‘bar’:
pr49754.c:3:28: warning: ‘y’ is used uninitialized [-Wuninitialized]
    3 | int bar () { int y; return +y; }
      |                            ^~

Returning larger aggregates started to be diagnosed in r245840.  Passing
structs to other functions by value still isn't diagnosed.  It used to be, but
it stopped  sometime between r143699 and r143725.

See pr95848 that tracks the problem with pass-by-value.

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

end of thread, other threads:[~2020-06-23 21:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-15  6:17 [Bug c/49754] New: Let gcc warn about all uninitialized variables giecrilj at stegny dot 2a.pl
2011-07-15  8:38 ` [Bug c/49754] " redi at gcc dot gnu.org
2011-07-15  9:36 ` giecrilj at stegny dot 2a.pl
2011-07-15  9:57 ` redi at gcc dot gnu.org
2011-07-15 10:16 ` redi at gcc dot gnu.org
2011-07-15 11:01 ` giecrilj at stegny dot 2a.pl
2012-10-04  8:23 ` [Bug middle-end/49754] Wuninitialized does not work with structs/unions/arrays manu at gcc dot gnu.org
2020-06-23 21:03 ` msebor at gcc dot gnu.org

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