7

Here is my code.

// test.c
#include <stdio.h>

#define ARRAY_SIZE 4

struct example_t {
  int field0;
  int field1;
  int field2;
  int field3;
  int field4;
};

struct example_t func(int *in, int array[ARRAY_SIZE]) {
  struct example_t out;
  return out;
}

int main() {
  int array[ARRAY_SIZE] = { 0, 0, 0, 0 };
  int a = 0;
  struct example_t col = func(&a, array);
  return 0;
}

gcc 11.1.0 gave

$ gcc test.c -o test
test.c: In function ‘main’:
test.c:22:26: warning: ‘func’ accessing 16 bytes in a region of size 4 [-Wstringop-overflow=]
   22 |   struct example_t col = func(&a, array);
      |                          ^~~~~~~~~~~~~~~
test.c:22:26: note: referencing argument 2 of type ‘int *’
test.c:14:18: note: in a call to function ‘func’
   14 | struct example_t func(int *in, int array[ARRAY_SIZE]) {
      |                  ^~~~

but g++ didn't.

I don't understand why the warning message is there, since there is no string operation in my code and I never read array in func.

If there are only 4 or fewer fields in struct example_t, GCC won't complain. Can someone please explain why is the message here and how can i fix it?

Thank you in advance.

  • @user3386109 Sorry about that, now I've updated the output of the compiler. – Tony Fettes Nov 09 '21 at 02:12
  • 1
    What happens if you initialize: `struct example_t out = {0};` – user3386109 Nov 09 '21 at 02:18
  • @user3386109 gcc gave the same diagnostics. – Tony Fettes Nov 09 '21 at 02:21
  • Other than warnings about unused variables, I see nothing wrong with that code. So the next step is to file a bug report. – user3386109 Nov 09 '21 at 02:26
  • 4
    Looks like https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101854 which is resolved in gcc12. Might still be worth logging with a reference to that bug in case it turns out to be different. – kaylum Nov 09 '21 at 02:31
  • I can reproduce the message (along with a bunch of others about unused parameters and variables and uninitialized values) using GCC 11.2.0 that I compiled from source on a RHEL 7.4 box. So, it isn't just your copy that has the problem. – Jonathan Leffler Nov 09 '21 at 02:36
  • 1
    FYI, godbolt also confirms that [gcc 11](https://godbolt.org/z/asqbEx5Ko) has the problem but [gcc trunk](https://godbolt.org/z/vvrTvs3nn) doesn't. – kaylum Nov 09 '21 at 02:44

0 Answers0