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

            Bug ID: 96564
           Summary: New maybe use of uninitialized variable warning since
                    GCC >10
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stefansf at linux dot ibm.com
  Target Milestone: ---

Consider the following MWE:

extern void* malloc (long unsigned int);
void fun (unsigned *x) {
  unsigned *a = malloc (*x);
  if (a == 0)
    return;
  if (a != x)            // (A)
    *a = *x;
  *x = *a;
}

If compiled with GCC 10, then no warning is emitted.

If compiled with GCC HEAD (currently 84005b8abf9), then the following warning
is emitted:

gcc -W -c -O2 mwe.c
mwe.c: In function 'fun':
mwe.c:8:8: warning: '*(a)' may be used uninitialized [-Wmaybe-uninitialized]
    8 |   *x = *a;
      |        ^~

Rational why this example is strictly speaking fine:

1) Assume x is a valid pointer.  Then malloc will either return a nullpointer
and we return, or a pointer to a fresh object which address is different from
any other existing object.  Thus (A) always evaluates to true which means *a is
initialized.

2) Assume x is an invalid pointer.  Then dereferencing x prior to the call to
malloc already results in UB.

Thus the only case in which condition (A) may evaluate to false is when x is an
invalid pointer (e.g. previously malloc'd and then free'd such that a further
call to malloc returns the very same address rendering (A) false) results in
UB.

Since this is a maybe warning I'm wondering whether this is considered a bug or
is acceptable.  Any thoughts?

Reply via email to