| Issue |
242842
|
| Summary |
[Clang] Wrong storage size for tentative array completed by later extern
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
metp
|
A tentative definition of an incomplete array whose size is completed by a later extern declaration is emitted with storage for only one element (the fallback for a never-completed array), while sizeof correctly uses the completed type. Writes within the declared bounds can go past the object:
```C
int b[];
extern int b[3];
int main(void) {
for (unsigned i = 0; i < sizeof b / sizeof b[0]; i++) /* sizeof b == 12, sizeof b[0] == 4 */
b[i] = 1; /* but storage is only created for b[0] */
return 0;
}
```
```bash
$ clang -fsanitize=address t.c && ./a.out
t.c:1:5: warning: tentative array definition assumed to have one element [-Wtentative-definition-array]
1 | int b[];
| ^
1 warning generated.
ERROR: AddressSanitizer: global-buffer-overflow ...
WRITE of size 4 ...
... is located 0 bytes after global variable 'b' defined in 't.c' ... of size 4
```
The warning seems wrong as well. The extern declaration completes the type of b. GCC emits 12 bytes.
Reproduces with Clang 3.8 through 23.1.0 and trunk, on every target tested (x86-64, i686, AArch64, ARMv7, RISC-V, MIPS64, PowerPC64le, s390x, LoongArch64, wasm32, BPF, Windows, macOS), at all optimization levels.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs