public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/99944] New: incorrect maybe-uninitialized warning on variable defined as an array
@ 2021-04-06 20:59 vincent-gcc at vinc17 dot net
  2021-04-07  7:25 ` [Bug c/99944] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2021-04-06 20:59 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99944
           Summary: incorrect maybe-uninitialized warning on variable
                    defined as an array
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

Consider the following testcase derived from the initial testcase of PR85777
and its cleaned-up testcase (which is actually a bit different since an enum
was replaced by an int, and it matters here).

int d;
int h(void);
void e1(void)
{
  int f[2];
  int g = 0;
  if (d)
    g++;
  if (d == 1)
    f[g++] = 2;
  (void) (f[0] || (g && h()));
}
void e2(void)
{
  enum { a } f[2];
  int g = 0;
  if (d)
    g++;
  if (d == 1)
    f[g++] = a;
  (void) (f[0] || (g && h()));
}

With a GCC snapshot built a few hour ago from the master branch:

cventin% gcc --version
gcc (GCC) 11.0.1 20210406 (experimental)
[...]

cventin% gcc -Werror=maybe-uninitialized -O2 -c file.c
file.c: In function ‘e1’:
file.c:11:3: error: ‘f[0]’ may be used uninitialized
[-Werror=maybe-uninitialized]
   11 |   (void) (f[0] || (g && h()));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
file.c: In function ‘e2’:
file.c:21:3: error: ‘*(unsigned int *)(&f[0])’ may be used uninitialized
[-Werror=maybe-uninitialized]
   21 |   (void) (f[0] || (g && h()));
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors

The error for e1 is correct, but not the one for e2 (for e2, previous GCC
versions were outputting ‘f’ instead of ‘*(unsigned int *)(&f[0])’, but this is
about the same thing).

cventin% gcc -Werror=maybe-uninitialized -O2 -c file.c -fsanitize=undefined
file.c: In function ‘e1’:
file.c:11:12: error: ‘f’ may be used uninitialized
[-Werror=maybe-uninitialized]
   11 |   (void) (f[0] || (g && h()));
      |           ~^~~
file.c:5:7: note: ‘f’ declared here
    5 |   int f[2];
      |       ^
file.c: In function ‘e2’:
file.c:21:12: error: ‘f’ may be used uninitialized
[-Werror=maybe-uninitialized]
   21 |   (void) (f[0] || (g && h()));
      |           ~^~~
file.c:15:14: note: ‘f’ declared here
   15 |   enum { a } f[2];
      |              ^
cc1: some warnings being treated as errors

Here, with the option -fsanitize=undefined added, both errors are incorrect.

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

* [Bug c/99944] incorrect maybe-uninitialized warning on variable defined as an array
  2021-04-06 20:59 [Bug c/99944] New: incorrect maybe-uninitialized warning on variable defined as an array vincent-gcc at vinc17 dot net
@ 2021-04-07  7:25 ` rguenth at gcc dot gnu.org
  2021-04-07  7:41 ` vincent-gcc at vinc17 dot net
  2021-04-14 16:38 ` [Bug c/99944] poor format of array reference in -Wuninitialized msebor at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-04-07  7:25 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
What's not correct with the diagnostic?  The obfuscated printing of f[0]?  Or
the misplaced caret?

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

* [Bug c/99944] incorrect maybe-uninitialized warning on variable defined as an array
  2021-04-06 20:59 [Bug c/99944] New: incorrect maybe-uninitialized warning on variable defined as an array vincent-gcc at vinc17 dot net
  2021-04-07  7:25 ` [Bug c/99944] " rguenth at gcc dot gnu.org
@ 2021-04-07  7:41 ` vincent-gcc at vinc17 dot net
  2021-04-14 16:38 ` [Bug c/99944] poor format of array reference in -Wuninitialized msebor at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: vincent-gcc at vinc17 dot net @ 2021-04-07  7:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
(In reply to Richard Biener from comment #1)
> What's not correct with the diagnostic?  The obfuscated printing of f[0]? 

Hmm... for the *(unsigned int *)(&f[0]) case, it is correct after all, though
it should just say f[0].

But in the case with -fsanitize=undefined, gcc says that f may be used
uninitialized. But f is automatically initialized; it is f[0] that may be used
uninitialized.

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

* [Bug c/99944] poor format of array reference in -Wuninitialized
  2021-04-06 20:59 [Bug c/99944] New: incorrect maybe-uninitialized warning on variable defined as an array vincent-gcc at vinc17 dot net
  2021-04-07  7:25 ` [Bug c/99944] " rguenth at gcc dot gnu.org
  2021-04-07  7:41 ` vincent-gcc at vinc17 dot net
@ 2021-04-14 16:38 ` msebor at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-04-14 16:38 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
           Severity|normal                      |minor
            Summary|incorrect                   |poor format of array
                   |maybe-uninitialized warning |reference in
                   |on variable defined as an   |-Wuninitialized
                   |array                       |
   Last reconfirmed|                            |2021-04-14
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
So the issues here are that a) the MEM_REF isn't formatted as neatly as it
could be, and b) that the warning says 'f' when it should mention 'f[0]'.  Let
me confirm this on that basis.

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

end of thread, other threads:[~2021-04-14 16:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-06 20:59 [Bug c/99944] New: incorrect maybe-uninitialized warning on variable defined as an array vincent-gcc at vinc17 dot net
2021-04-07  7:25 ` [Bug c/99944] " rguenth at gcc dot gnu.org
2021-04-07  7:41 ` vincent-gcc at vinc17 dot net
2021-04-14 16:38 ` [Bug c/99944] poor format of array reference in -Wuninitialized 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).