public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/96629] New: spurious uninitialized variable warning with branches at -O1 and higher
@ 2020-08-16  6:10 yyc1992 at gmail dot com
  2020-08-25 10:20 ` [Bug c/96629] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: yyc1992 at gmail dot com @ 2020-08-16  6:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96629
           Summary: spurious uninitialized variable warning with branches
                    at -O1 and higher
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yyc1992 at gmail dot com
  Target Milestone: ---

Reduced test code:

```
int mem(char *data);
int cond(void);
void f(char *data, unsigned idx, unsigned inc)
{
    char *d2;
    int c = cond();
    if (idx >= 2) {
        if (c)
            d2 = data;
        mem(data);
    }
    else if (inc > 3) {
        if (c)
            d2 = data;
        mem(data);
    }
    else {
        if (c) {
            d2 = data;
        }
    }
    if (*data) {
    }
    else if (c) {
        mem(d2);
    }
}
```

Compiling with `gcc -Wall -Wextra -O{1,2,s,3,fast}` warns about

```
a.c: In function 'f':
a.c:27:9: warning: 'd2' may be used uninitialized in this function
[-Wmaybe-uninitialized]
   27 |         mem(d2);
      |         ^~~~~~~
```

However, it should be clear that `d2` is always assigned when `c` is true. In
fact, it seems that GCC could figure this out in some cases. Changes that can
surpress the warning includes,

1. Remove any of the `mem(data)` calls.
2. Remove any one of the `if`s (leaving only the if or else branch
unconditionally)
3. Change first condition to be on inc instead.
4. Removing the last `*data` branch.

Version tested:
AArch64: 10.2.0
ARM: 9.1.0
x86_64: 10.1.0
mingw64: 10.2.0

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

end of thread, other threads:[~2022-11-20  4:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-16  6:10 [Bug c/96629] New: spurious uninitialized variable warning with branches at -O1 and higher yyc1992 at gmail dot com
2020-08-25 10:20 ` [Bug c/96629] " rguenth at gcc dot gnu.org
2020-09-03 18:13 ` [Bug c/96629] spurious maybe uninitialized variable warning with difficult control-flow analysis manu at gcc dot gnu.org
2020-09-03 18:21 ` yyc1992 at gmail dot com
2022-11-20  4:50 ` [Bug tree-optimization/96629] " law 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).