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

            Bug ID: 128711
           Summary: [14/15/16 Regression] -Os -Wmaybe-uninitialized: false
                    positive for a struct copy read through
                    const-attributed inline helpers after an intervening
                    pointer-write call
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: leo at yuriev dot ru
  Target Milestone: ---

Tested with gcc 16.2.1 (Red Hat 16.2.1-3), 16.2.0, 16.0.1 (trunk 20260322),
15.2/15.3 and 14.x. gcc 13.4 does NOT produce the diagnostic for this exact
case (the same sources under -flto -Os on a full-project build do fail there).

At -Os gcc reports 'previous.a' and 'previous.b' may be used uninitialized for
the local struct copy `previous`:

$ cat repro.c
typedef struct { unsigned long long a, b; } fileid_t;
struct env { fileid_t lck_fileid; };
__attribute__((const)) static inline int is_set(const fileid_t *id) { return
id->a || id->b; }
__attribute__((const)) static inline int eq(const fileid_t *x, const fileid_t
*y) {
  return x->a == y->a && x->b == y->b;
}
int by_fd(fileid_t *id);
int lck_setup(struct env *e) {
  const fileid_t previous = e->lck_fileid;   /* copy of a defined field */
  int err = by_fd(&e->lck_fileid);           /* unrelated pointer write */
  if (err)
    return err;
  return (is_set(&previous) && !eq(&e->lck_fileid, &previous)) ? 42 : 0;
}

$ cat driver.c
#include <string.h>
typedef struct { unsigned long long a, b; } fileid_t;
struct env { fileid_t lck_fileid; };
int lck_setup(struct env *e);
int by_fd(fileid_t *id) {
  id->a = 1;
  id->b = 2;
  return 0;
}
int main(void) {
  struct env e;
  memset(&e, 0, sizeof(e));
  return lck_setup(&e);   /* 0 = the value behaves as defined */
}

$ gcc -Os -Werror=maybe-uninitialized -c repro.c
repro.c: In function 'lck_setup':
repro.c:3:84: error: 'previous.a' may be used uninitialized
[-Werror=maybe-uninitialized]
  3 | __attribute__((const)) static inline int is_set(const fileid_t *id) {
return id->a || id->b; }
    |                                                                          
   ~~~~~~^~~~~~~~
repro.c:9:18: note: 'previous.a' was declared here
    9 |   const fileid_t previous = e->lck_fileid;
      |                  ^~~~~~~~
repro.c:3:84: error: 'previous.b' may be used uninitialized
[-Werror=maybe-uninitialized]
    3 | __attribute__((const)) static inline int is_set(const fileid_t *id) {
return id->a || id->b; }

Expected: no diagnostic.

Analysis:
- The driver zero-initializes `*e`, so `e->lck_fileid` and the copy `previous`
are defined before the reads; the reads are additionally guarded by
`is_set(&previous)`.
- `by_fd` writes a different object (`e->lck_fileid`, not `previous`), so the
copy cannot be clobbered.
- The diagnostic is triggered by __attribute__((const)) on the two
static-inline helpers: removing it from either helper silences the report; a
const-attributed non-inline function does not trigger.
- -flto is not required for this shape; under -flto the same class also fires
through a C++ wrapper chain (by-value parameters passed by address to an inline
helper) and for callee-filled stat structures (307 instances in one build of
the project).

Version matrix for the minimal case (-Os, no LTO):
  gcc 13.4:  clean
  gcc 14.x:  FAILS
  gcc 15.2:  FAILS
  16.0.1 (trunk 20260322): FAILS
  16.2.0:    FAILS
  16.2.1:    FAILS

Real-world occurrence: libmdbx (github.com/Mithril-mine/libmdbx-devel) fails
its Release CI build at the first translation unit with the same diagnostics
for src/lck.c:190-206 (the `previous = env->lck_fileid` pattern with
const-attributed osal_fileid_is_set/osal_fileid_eq from src/osal/osal.h).

Reply via email to