I have a series of about 30 functions which use local arrays as such:
void foo() {
const int array_size = 32;
char my_array[array_size];
// .. do stuff
// (array_size is used multiple times)
}
The code (when compiling with -Wstack-protector
) will produce the following warning message:
warning: stack protector not protecting local variables: variable length buffer
So I have two questions:
First, why is my_array
considered variable length? Yea, I know why technically, but shouldn't the compiler be smart enough to realize that it isn't really variable length?
Second, what is the most appropriate way to fix this warning? I know I can fix it by doing something like:
void foo() {
char my_array[32];
const int array_size = sizeof(my_array) / sizeof(my_array[0]);
// .. do stuff
// (array_size is used multiple times)
}
but is there a better, more "correct" way?