Skip to contentSkip to conversation

clang mis-compiles "x"[2]

DISCUSSION David Laight 2 participants 2 messages Aug 28, 2023

lore
Share & tools
lore
#0 lore raw

Clang mis-compiles:

int foo(void) { return "a"[2]; }

to be just a 'ret' instruction.

While clearly non-sensical returning the input value of %rax doesn't make any sense.

gcc generates a real string and then returns the byte after the '\0' - which is (sort of) what the code asks for.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#1 lore raw
Quoted 5 lines · David Laight

Clang mis-compiles:

int foo(void) { return "a"[2]; }

Quoted 7 lines

to be just a 'ret' instruction.

While clearly non-sensical returning the input value of %rax doesn't make any sense.

gcc generates a real string and then returns the byte after the '\0' - which is (sort of) what the code asks for.

Reports of what code is generated when there is very clearly UB are inappropriate for this list.

I encourage you to inspect the output of -fsanitize=undefined before making such reports to compiler vendors.

https://godbolt.org/z/o4E6o33fP

Perhaps you're conflating the address of one past the end of an object being defined, vs the value one past the end of an object which is not.

Quoted 8 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

Signature · 2 lines
Thanks,
~Nick Desaulniers