[Bug c/85800] A miscompilation bug with unsigned char
juneyoung.lee at sf dot snu.ac.kr
gcc-bugzilla@gcc.gnu.org
Wed May 16 08:34:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85800
--- Comment #2 from Juneyoung Lee <juneyoung.lee at sf dot snu.ac.kr> ---
If address is not adjacent - you can reorder the definition of A and B and try
again.
```
int A[4], B[4];
```
If changing
if (c1 == c2)
// Always true if p and q have same integer representation.
arr3[i] = arr1[i];
else
arr3[i] = arr2[i];
to
if (c1 == c2)
// Always true if p and q have same integer representation.
arr3[i] = arr2[i];
else
arr3[i] = arr1[i];
removed miscompilation, I think this implies that the former one is folded into
`arr[3] = arr2[i]` (which is incorrect, because arr2 is equivalent to `q`) but
the latter one is folded into `arr[3] = arr1[i]`.
This explains the generated assembly as well: Compiling store_10_to_p with -O3
generates
```
store_10_to_p(int*, int*):
mov DWORD PTR [rdi], 1
mov DWORD PTR [rsi], 10
ret
```
meaning that it is actually trying to store 10 to *q, which is UB if the
function is inlined. (B[4] is out-of-bounds)
More information about the Gcc-bugs
mailing list