GCC undefined behaviors are getting wild
http://blog.pkh.me/p/37-gcc-undefined-behaviors-are-getting-wild.html
Discussions: https://discu.eu/q/http://blog.pkh.me/p/37-gcc-undefined-behaviors-are-getting-wild.html
GCC undefined behaviors are getting wild
http://blog.pkh.me/p/37-gcc-undefined-behaviors-are-getting-wild.html
Discussions: https://discu.eu/q/http://blog.pkh.me/p/37-gcc-undefined-behaviors-are-getting-wild.html
@c_discussions This is exceptionally scary! Worst part? Won't happen with optimizations disabled.
https://godbolt.org/z/vM7nbdzzz
Thanks for sharing!
Edit: Appears to be a gcc-12 feature...
uint8_t tab[0x1ff + 1]; uint8_t f(int32_t x) { if (x < 0) return 0; int32_t i = x * 0x1ff / 0xffff; if (i >= 0 && i < sizeof(tab)) { printf("tab[%d] looks safe because %d is between [0;%d[\n", i, i, (int)sizeof(tab)); return tab[i]; } return 0; } int main(int ac, char **av) { return f(50000000); }