1

Minimal code:

#include  <stdio.h>
#include <stdint.h>

// gcc test.c
// gcc 11.2.1 20210816 [revision 056e324ce46a7924b5cf10f61010cf9dd2ca10e9]
// bug
// test.c:41:5: Warning: «test_bug» accessing 8 bytes in a region of size 4 [-Wstringop-overflow=]


typedef struct test_struct
{
  int type;
  const char *file; // removing this fix warning
  int line; // removing this fix warning
} test_struct;


// warning only when ext_names is second argument
test_struct test_bug(uint32_t *queue_info_count, const char *ext_names[]){
  test_struct retval = (test_struct){ .type = 0,};
  printf("test_bug %s\n",ext_names[*queue_info_count]);
  return retval;
}

// no warning
test_struct test_no_bug(const char *ext_names[], uint32_t *queue_info_count){
  test_struct retval = (test_struct){ .type = 0,};
  printf("test_no_bug %s\n",ext_names[*queue_info_count]);
  return retval;
}

int main(void)
{
  printf("Hello, world!\n");

  uint32_t queue_info_count = 0;
  const char *extension_names[] = {
    "test",
  };

  test_bug(&queue_info_count, extension_names); // warning

  test_no_bug(extension_names, &queue_info_count); // no warning

  return 0;
}

Compiling this code with GCC 11 always show warning on line 41 test_bug. Clang and other versions of GCC do not show any warnings.

Is this a bug? How to make this code correctly then? Thanks.

Original code where I got this warning is very large, and test_bug function used hundreds of times from many parts of code, and there just single warning in middle of logic.

Danil S
  • 67
  • 7

1 Answers1

1

This was a GCC bug, but it is already fixed in the trunk version https://gcc.godbolt.org/z/3s6haqvWP

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Danil S
  • 67
  • 7