public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/98900] New: Wrong static analyses warning when optimisation is on
@ 2021-01-31 12:23 oleg.pekar.2017 at gmail dot com
  2021-01-31 21:32 ` [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2 msebor at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: oleg.pekar.2017 at gmail dot com @ 2021-01-31 12:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98900
           Summary: Wrong static analyses warning when optimisation is on
           Product: gcc
           Version: 8.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: oleg.pekar.2017 at gmail dot com
  Target Milestone: ---

Created attachment 50095
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50095&action=edit
Preprocessor output for source file test1.c

gcc version: 8.3.1
system type: RedHat 8.1, Kernel 4.18.0-147.3.1.el8_1.x86_64

Problem: Wrong static analyses warning when optimisation is on.
Input source: attached test1.i
Description: in this code address of a variable local to a block is used
outside of the block. With no optimisation there's no warning while the code
still can cause a problem. But with optimisation static analyses provides a
confusing warning - it complains about a variable not used in the context of
the warning.

Command line with no optimisation: gcc -Wall -save-temps test1.c
Output: nothing

Command line with optimisation: gcc -Wall -save-temps -O2 test1.c
Output: 
test1.c: In function ‘main’:
test1.c:11:7: warning: ‘x’ is used uninitialized in this function
[-Wuninitialized]
     g = *p;
     ~~^~~~

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

* [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2
  2021-01-31 12:23 [Bug c/98900] New: Wrong static analyses warning when optimisation is on oleg.pekar.2017 at gmail dot com
@ 2021-01-31 21:32 ` msebor at gcc dot gnu.org
  2021-01-31 22:00 ` msebor at gcc dot gnu.org
  2022-02-08 23:13 ` msebor at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-31 21:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |24639
                 CC|                            |msebor at gcc dot gnu.org
          Component|c                           |middle-end
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
            Summary|Wrong static analyses       |misleading -Wuninitialized
                   |warning when optimisation   |using a variable outside
                   |is on                       |its lifetime with -O2
   Last reconfirmed|                            |2021-01-31
           Keywords|                            |diagnostic

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
The warning could be clearer but at the point in the pipeline it's issued there
isn't enough information in the IL to differentiate the specific nature of the
problem (variable used after the end of its lifetime) from a plain
uninitialized read:

$ gcc -O1 -S -Wall -fdump-tree-uninit=/dev/stdout pr98850.c

;; Function main (main, funcdef_no=0, decl_uid=1943, cgraph_uid=1,
symbol_order=1) (executed once)

test1.c: In function ‘main’:
test1.c:11:7: warning: ‘x’ is used uninitialized [-Wuninitialized]
int main ()
{
  int x;

  <bb 2> [local count: 1073741824]:
  g = x_3(D);
  return 0;

}

But after changing the example to the one below doesn't make the warning more
informative even though the IL now has sufficient information (the CLOBBER) to
make it clear the variable isn't uninitialized at the time it's read but rather
its lifetime has ended.  So with that I agree that things can be improved.

$ cat pr98850.c && gcc -O1 -S -Wall -fdump-tree-uninit=/dev/stdout pr98850.c
int g = 10;

void f (int*);

int main()
{
    int* p = 0;
    {
        int x = 15;
        p = &x;
        f (p);
    }

    g = *p;
}

;; Function main (main, funcdef_no=0, decl_uid=1945, cgraph_uid=1,
symbol_order=1) (executed once)

pr98850.c: In function ‘main’:
pr98850.c:14:9: warning: ‘x’ is used uninitialized [-Wuninitialized]
   14 |     g = *p;
      |         ^~
pr98850.c:9:13: note: ‘x’ declared here
    9 |         int x = 15;
      |             ^
int main ()
{
  int x;
  int _1;

  <bb 2> [local count: 1073741824]:
  x = 15;
  f (&x);
  x ={v} {CLOBBER};
  _1 = x;
  g = _1;
  return 0;

}


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=24639
[Bug 24639] [meta-bug] bug to track all Wuninitialized issues

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

* [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2
  2021-01-31 12:23 [Bug c/98900] New: Wrong static analyses warning when optimisation is on oleg.pekar.2017 at gmail dot com
  2021-01-31 21:32 ` [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2 msebor at gcc dot gnu.org
@ 2021-01-31 22:00 ` msebor at gcc dot gnu.org
  2022-02-08 23:13 ` msebor at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-01-31 22:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=81524

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
See also pr81524 for a superset of these problems.

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

* [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2
  2021-01-31 12:23 [Bug c/98900] New: Wrong static analyses warning when optimisation is on oleg.pekar.2017 at gmail dot com
  2021-01-31 21:32 ` [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2 msebor at gcc dot gnu.org
  2021-01-31 22:00 ` msebor at gcc dot gnu.org
@ 2022-02-08 23:13 ` msebor at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-02-08 23:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
GCC 12 issues -Wdangling-poinnter for this bug now at all optimization levels. 
It still issues -Wuninitialized at -O1 and above, but with the dangling pointer
warning I don't think it's a big deal.  It should of course still be possible
to suppress it.

$ cat pr98900.c && gcc -S -Wall pr98900.c
int g = 10;

int main()
{
    int* p = 0;
    {
        int x = 15;
        p = &x;
    }

    g = *p;

    return 0;
}
pr98900.c: In function ‘main’:
pr98900.c:11:9: warning: using dangling pointer ‘p’ to ‘x’
[-Wdangling-pointer=]
   11 |     g = *p;
      |         ^~
pr98900.c:7:13: note: ‘x’ declared here
    7 |         int x = 15;
      |             ^

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

end of thread, other threads:[~2022-02-08 23:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-31 12:23 [Bug c/98900] New: Wrong static analyses warning when optimisation is on oleg.pekar.2017 at gmail dot com
2021-01-31 21:32 ` [Bug middle-end/98900] misleading -Wuninitialized using a variable outside its lifetime with -O2 msebor at gcc dot gnu.org
2021-01-31 22:00 ` msebor at gcc dot gnu.org
2022-02-08 23:13 ` 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).