https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115437
Bug ID: 115437
Summary: Wrong optimized code - GCC didn't detect a variable is
modified
Product: gcc
Version: 12.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: other
Assignee: unassigned at gcc dot gnu.org
Reporter: Explorer09 at gmail dot com
Target Milestone: ---
Note: I cannot tell which component this bug belongs to. Please help me update
the "component" field for this bug report.
Tested in Compiler Explorer: https://godbolt.org/z/c6fKjMboK
## Test code
```c
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
__attribute__((noinline)) \
uint32_t utf8_to_ascii(const uint8_t **sequence)
{
if (**sequence <= 0x7F) {
return *(*sequence)++;
}
(*sequence)++;
return (uint32_t)-1;
}
int main(void)
{
static const char str[] = { 'A', 'B', 'C', '\x00' };
static const uint8_t str2[] = { 'A', 'B', 'C', '\x00' };
uint32_t code_point;
{
const char *str_ptr;
str_ptr = str;
code_point = utf8_to_ascii((const uint8_t **)&str_ptr);
printf("%p %p\n", str, str_ptr);
printf("src: %lu byte(s) parsed\n", (unsigned long)(str_ptr - str));
}
{
const uint8_t *str_ptr2;
str_ptr2 = (const uint8_t *)str;
code_point = utf8_to_ascii(&str_ptr2);
const char *str_ptr;
str_ptr = (const char *)str_ptr2;
printf("%p %p\n", str, str_ptr);
printf("src: %lu byte(s) parsed\n", (unsigned long)(str_ptr - str));
}
{
const uint8_t *str_ptr2;
str_ptr2 = str2;
code_point = utf8_to_ascii(&str_ptr2);
printf("%p %p\n", str2, str_ptr2);
printf("src: %lu byte(s) parsed\n", (unsigned long)(str_ptr2 - str2));
}
return 0;
}
```
## Expected result
All three blocks in main() should print "src: 1 byte(s) parsed".
## Actual result
x86_64 gcc 12.1 with "-O2" option would have the first block print "src: 0
byte(s) parsed". "-Os" would also produce the same error result.
"-O0" and "-O1" are fine as far as I have tested.
GCC versions 12.1 to 14.1 are those that are affected.