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

            Bug ID: 117490
           Summary: Invalid TBAA for structures without tag and identical
                    definition in C.
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: keinfluegeltier at posteo dot de
  Target Milestone: ---

Taken from https://github.com/llvm/llvm-project/issues/115252:

typedef struct {
  int i1;
} s1;

typedef struct {
  int i1;
} s2_alt;

int f2(s1 *s1p, s2_alt *s2p) {
  s1p->i1 = 2;
  s2p->i1 = 3;
  return s1p->i1 * 3;
}

With -O2 or higher GCC constant-folds the return value to 6.

This is not correct in C. If another translation unit contains

typedef struct {
  int i1;
} s3;

then a call

s3 x;
f2(&x, &x);

must return 9.

This is because s3 in the second translation unit is compatible with both s1
and s2_alt in the first translation unit and therefore access through both s1
and s2_alt is not an aliasing violation. See the rules for cross-translation
unit type compatibility of structures without tags in (N3220) ยง6.2.7.

I think in C++ the optimization is valid, because type identity is instead
established by the (first) typedef name.

Reply via email to