Issue 241280
Summary [clang][Sema] #embed followed by an array designator overrides the wrong element (wrong code at all -O levels)
Labels clang
Assignees
Reporter jeffchen006
    `int a[4] = { #embed (3 bytes), 9, [1] = 0 };` must give `{47, 0, 32, 9}`. Clang keeps the unexpanded `#embed` as one structured-list entry, so `[1]` indexes the structured list and replaces the `9`. The result is `{47, 42, 32, 0}` at every -O level (the clang warning also points at the `9`).

**Reproducer** (`repro.c`)
```c
/* #embed reads the first 3 bytes of this file: '/', '*', ' ' = 47, 42, 32. So a = {47, 42, 32, 9},
   then [1] = 0 overrides a[1] (C23 6.10.4, 6.7.11): a = {47, 0, 32, 9}; grows(2147483605) = 1.
   Correct output: "47 0 32 9" and "1". clang (all -O levels) makes a = {47, 42, 32, 0}: the 0 replaces the 9. */
int printf(const char *, ...);
static int grows(int x) { return x + 1 > x; }
int main(void) {
  int a[] = {
#embed __FILE__ limit(3)
  , 9, [1] = 0 };
  printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
  printf("%d\n", grows(2147483605 + a[1]));
  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 -std=c23 -O0 repro.c -o ./o0
repro.c:9:14: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
    9 |   , 9, [1] = 0 };
      | ^
repro.c:9:5: note: previous initialization is here
    9 | , 9, [1] = 0 };
      |     ^
1 warning generated.
$ ./o0
47 42 32 0
0
$ clang -std=c23 -O3 repro.c -o ./o3
(same warning as above)
$ ./o3
47 42 32 0
1
```
Correct: `47 0 32 9` / `1`. Clang `-O0`: `47 42 32 0` / `0`; `-O3`: `47 42 32 0` / `1`.

**Why the expected output is right**
`#embed` gives 47, 42, 32; then `9` is `a[3]`; `[1] = 0` overrides `a[1]` (C23 6.7.11p19). No UB in the source.

**How the -O0/-O3 difference arises**
The front-end bug is wrong at all levels. The second output line comes from a `grows()` signed-overflow check that overflows only with the wrong `a[1] = 42`, so -O0 and -O3 differ. UBSan reports that overflow, but with the correct `a[1] = 0` there is no overflow.

**Potential Root cause**
`clang/lib/Sema/SemaInit.cpp`, `InitListChecker::CheckArrayType`: after an `EmbedExpr`, `elementIndex` advances by the data count (3), but the structured list gets ONE entry for the whole `EmbedExpr` (`HandleEmbed` fast path), so `StructuredIndex` advances by 1. A later array designator `[1]` sets the structured index from the array index. AST: `InitListExpr 'int[4]'` = { `EmbedExpr ... 3 elements`, `IntegerLiteral 0` }: the 9 is gone.

**Possible Proposed fix**
Simplest: when the init list has any designator, do not use the multi-element fast path in `HandleEmbed` (one structured entry per data element). Better: when a designator selects an index inside a multi-element `EmbedExpr` entry, split that entry so structured indices equal array indices again.

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

Reply via email to