Issue |
148830
|
Summary |
Clang optimizer produces incorrect arithmetic result with global pointers in loops
|
Labels |
clang
|
Assignees |
|
Reporter |
Dong-hui-li
|
## Description
When compiling code with global pointer operations and type casting in loops, Clang optimizer produces incorrect arithmetic results at -O2 optimization level.
## Environment
- **Clang version**: 14.0.0
- **Operating System**: Linux
- **CPU**: x86_64
- **Optimization level**: -O2
- **GCC version**: 11.4.0 (for comparison)
## Steps to Reproduce
1. Create `test.c` file with the following content:
```
#include <stdio.h>
#include <string.h>
int *a;
int b;
int main() {
char d[16];
memset(d, 170, 16);
[clang_bug.zip](https://github.com/user-attachments/files/21232124/clang_bug.zip)
for (int c = 0; c < 16; c++) {
a = (int*)&d[c];
b = *a + c;
}
printf("Result: %d\n", b);
return 0;
}
```
2. Compile with Clang: clang-14 -O2 test.c -o test_clang
3. Run the program: ./test_clang
4. Output: Result: 185
5. Compile with GCC (for comparison):gcc -O2 test.c -o test_gcc
6. Run the GCC version:./test_gcc
7. Output: Result: 1048761
## Expected Result
Consistent output matching GCC: `Result: 1048761`
## Actual Result
Clang outputs incorrect result: `Result: 185`
## Additional Notes
1. The issue occurs only at optimization level -O2 and above
2. GCC produces consistent results across all versions
3. This miscompilation may affect security-critical applications
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs