public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109550] New: warning: "p" may be used uninitialized
@ 2023-04-19  6:58 f.heckenbach@fh-soft.de
  2023-04-19  8:03 ` [Bug c/109550] " rguenth at gcc dot gnu.org
  2023-04-19  8:51 ` f.heckenbach@fh-soft.de
  0 siblings, 2 replies; 3+ messages in thread
From: f.heckenbach@fh-soft.de @ 2023-04-19  6:58 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109550
           Summary: warning: "p" may be used uninitialized
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.heckenbach@fh-soft.de
  Target Milestone: ---

% cat test.c
#include <stdlib.h>

int f (char const *p);

void a (void)
{
  char const *p = alloca (1);
  f (p);
}
% gcc -c -Wall test.c
test.c: In function "a":
test.c:8:3: warning: "p" may be used uninitialized [-Wmaybe-uninitialized]
    8 |   f (p);
      |   ^~~~~
test.c:3:5: note: by argument 1 of type "const char *" to "f" declared here
    3 | int f (char const *p);
      |     ^

It also happens with "malloc" instead of "alloca", but not with a normal
function.

The warning disappears when removing "const" in both places.

Ironically, when trying to work around the warning, I noticed that wrapping
alloca in another function gets rid of the warning. However, that would
actually be wrong (dangling pointer).

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

* [Bug c/109550] warning: "p" may be used uninitialized
  2023-04-19  6:58 [Bug c/109550] New: warning: "p" may be used uninitialized f.heckenbach@fh-soft.de
@ 2023-04-19  8:03 ` rguenth at gcc dot gnu.org
  2023-04-19  8:51 ` f.heckenbach@fh-soft.de
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-19  8:03 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |24639

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
The idea is that when you pass a const qualified pointer to a function you
are expected to read from it, not only use the pointer as value.  It's
a heuristic that works as designed.  You can add
  __attribute__((access(none, 1)))
to the function 'f' to tell the compiler it is in fact not inspecting the
memory pointed to by the first argument.


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] 3+ messages in thread

* [Bug c/109550] warning: "p" may be used uninitialized
  2023-04-19  6:58 [Bug c/109550] New: warning: "p" may be used uninitialized f.heckenbach@fh-soft.de
  2023-04-19  8:03 ` [Bug c/109550] " rguenth at gcc dot gnu.org
@ 2023-04-19  8:51 ` f.heckenbach@fh-soft.de
  1 sibling, 0 replies; 3+ messages in thread
From: f.heckenbach@fh-soft.de @ 2023-04-19  8:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Frank Heckenbach <f.heckenbach@fh-soft.de> ---
It might be useful then to actually say to in the warning, e.g. "p[...] may be
used uninitialized". This might have saved me some debugging effort.

But actually, that's not all. This code still gives the warning, although the
memory pointed to is now initialized:

#include <stdlib.h>

void f (int, char const *);
void g (int);

void a (int n, char const *s)
{
  char *p = alloca (n);
  for (int i = 0; i < n; i++)
    p[i] = s[i];
  f (n, p);
  for (int i = 0; i < n; i++)
    g (i);
}

Though this time, the warning disappears when I remove the unrelated code below
(with the call to g). Why is that?

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

end of thread, other threads:[~2023-04-19  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-19  6:58 [Bug c/109550] New: warning: "p" may be used uninitialized f.heckenbach@fh-soft.de
2023-04-19  8:03 ` [Bug c/109550] " rguenth at gcc dot gnu.org
2023-04-19  8:51 ` f.heckenbach@fh-soft.de

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