Issue 241279
Summary [clang][CodeGen] Designated-init update zero-fills a bit-field set by the base (DR 413) (wrong code at all -O levels)
Labels clang
Assignees
Reporter jeffchen006
    `struct Out o = { .s = v, .s.c = 5 };` must keep `o.s.b` from `v` (C DR 413). Clang copies `v`, then the updater list zero-fills the bit-field storage unit, so `o.s.b` becomes 0 at every -O level.

**Reproducer** (`repro.c`)
```c
/* C17 6.7.9p19 (WG14 DR 413): ".s.c = 5" overrides only o.s.c, so o.s.b keeps 2.
   Correct output: "2" and "1" (x = 2147483645, so x + 1 > x).
 clang at every -O level sets o.s.b to 0; then -O0 prints "0 / 0" and -O3 "0 / 1". */
int printf(const char *, ...);
struct In { unsigned b : 4; int c; };
struct Out { struct In s; };
static int grows(int x) { return x + 1 > x; }
int main(void) {
  struct In v = { 2, 0 };
  struct Out o = { .s = v, .s.c = 5 };
  printf("%u\n", o.s.b);
  printf("%d\n", grows(2147483647 - (int)o.s.b));
  return 0;
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 repro.c -o ./o0
repro.c:10:30: warning: initializer partially overrides prior initialization of this subobject [-Winitializer-overrides]
   10 | struct Out o = { .s = v, .s.c = 5 };
      | ^~~~~~
repro.c:10:25: note: previous initialization is here
   10 | struct Out o = { .s = v, .s.c = 5 };
      |                         ^
1 warning generated.
$ ./o0
0
0
$ clang -O3 repro.c -o ./o3
(same warning as above)
$ ./o3
0
1
```
Correct: `2` / `1`. Clang `-O0`: `0` / `0`; `-O3`: `0` / `1`.

**Why the expected output is right**
C17 6.7.9p19 and DR 413: a later designator for `.s.c` overrides only that member; `o.s.b` keeps the value from `.s = v`. No UB in the source.

**How the -O0/-O3 difference arises**
The front-end bug (b = 0) is wrong at all levels. The second output line comes from a `grows()` overflow check: with the wrong b = 0 it overflows, so -O0 and -O3 differ. UBSan reports that overflow, but it exists only because of the wrong value; with the correct b = 2 there is no overflow.

**Potential Root cause**
`clang/lib/CodeGen/CGExprAgg.cpp`: `AggExprEmitter::VisitDesignatedInitUpdateExpr` emits the base (`memcpy` from `v`), then `VisitInitListExpr(E->getUpdater())`. In `VisitCXXParenListOrInitListExpr`, `ZeroInitPadding` is true for C, and for each field `DoZeroInitPadding` runs before the initializer; for a bit-field it stores 0 to the whole storage unit. The initializer is a `NoInitExpr`, which writes nothing, so b = 2 from the base is lost. (From 627746581b8f, #97121 / #110051.)

**Possible Proposed fix**
Do not zero padding or bit-field storage while the emitter visits the updater of a `DesignatedInitUpdateExpr`:
```diff
 EmitInitializationToLValue(E->getBase(), DestLV);
- VisitInitListExpr(E->getUpdater());
+  llvm::SaveAndRestore InUpdater(IsDesignatedInitUpdater, true);
+ VisitInitListExpr(E->getUpdater());
 ...
-  const bool ZeroInitPadding = CGF.CGM.shouldZeroInitPadding() && !Dest.isZeroed();
+  const bool ZeroInitPadding = CGF.CGM.shouldZeroInitPadding() && !Dest.isZeroed() &&
+ !IsDesignatedInitUpdater;
```
(Check the same pattern in CGExprConstant.cpp.)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to