Search code examples
cgccclanggcc-warning

GCC : Static array index in function argument doesn't trigger any warning


I'm trying to understand the use of the "static" keyword as an array index in a function declaration in C.

After reading this article, I tried to declare such a function, and purposefully passing it an array that is too short:

#include <stdio.h>
#include <stdlib.h>


void print_string10(char string10[static 10]) {

// Should trigger a warning if the argument is NULL or an array of less than 10 elements

    printf("%s\n",string10);

}

int main(void) {

    char short_string[] = "test";

    print_string10(short_string); // should trigger a warning as the string is 5 long

    return EXIT_SUCCESS;
}

Compiling with clang as in the article triggers the warning, but gcc -Wall -Werror does not, it compiles and run fine.

I couldn't find an explanation, is that a normal behaviour for GCC to omit this warning?


Solution

  • It looks like this is a bug in GCC. It seems there were some disagreements regarding whether or not this should be reported at compile time. Nonetheless, it was accepted as a bug but there doesn't seem to be a priority on fixing it.

    This is detailed in bug report 50584. Comment 9 in particular states:

    (In reply to Malcolm Inglis from comment #4)

    Could someone change the status of this bug?

    Notwithstanding whether the feature is pretty or ugly, GCC does accept this code and warnings for undefined behavior, when possible, are desired. Thus, confirmed.

    This does not mean anyone is going to work on fixing this. Please, if you are using this feature and would like to see this warning in GCC, please consider contributing it: https://gcc.gnu.org/wiki/GettingStarted#Basics:_Contributing_to_GCC_in_10_easy_steps

    If you start working on this, it would be good to say so here.