I have a question about array index bounds check that I do not fully understand. Compiling the code below, I get warnings for out-of-bounds in one case, but not the other, also printf output gets different, but should be same? Maybe someone can explain what is going on, and if observed behavior is as expected. BR Fredrik
SOURCE CODE #include <stdio.h> static const char *item[] = { "0" }; const char * __attribute__ ((noinline)) get_item_at(int idx) { return item[idx]; } int main(void) { printf("Testing string array auto-contcat\n"); // this gives warning printf("item_at_0 = %s\n", item[0]); printf("item_at_1 = %s\n", item[1]); // this gives no warning, and other output <<< ??? printf("item_at_0 = %s\n", get_item_at(0)); printf("item_at_1 = %s\n", get_item_at(1)); return 0; } COMPILATION (gets warnings) gcc -o test string_array_concat.c -W -Wall -Wextra -Os In file included from /usr/include/stdio.h:867, from string_array_concat.c:1: In function ‘printf’, inlined from ‘main’ at string_array_concat.c:19:3: /usr/include/x86_64-linux-gnu/bits/stdio2.h:107:10: warning: ‘%s’ directive argument is null [-Wformat-overflow=] 107 | return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ()); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ EXECUTION (prints different results) Testing string array auto-contcat item_at_0 = 0 item_at_1 = (null) item_at_0 = 0 item_at_1 = 0