LINUX.ORG.RU

GCC 11 дает предупреждение только при определенном порядке аргументов, баг?

 


0

1

Код:

#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;
}

Помогите понять в чем «ошибка» и ошибка ли это, спасибо.

test_struct retval = (test_struct){ .type = 0,};

на такие штуки 20 стандарт плюсов ругается, это откуда-то из це?

error: ISO C++ forbids compound-literals [-Wpedantic]
   34 |   test_struct retval = (test_struct){ .type = 0,};
      |                                                 ^
warning: missing initializer for member ‘test_struct::file’ [-Wmissing-field-initializers]
warning: missing initializer for member ‘test_struct::line’ [-Wmissing-field-initializers]
alysnix ★★★
()
Ответ на: комментарий от alysnix

Он ругается на не полный список designated initializers, они в плюсах требуют указывать все поля. А сама конструкция сишная, это compound literal.

xaizek ★★★★★
()

в gcc 9.3.0 не ругается. кроме этих инитиалайзеров

alysnix ★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.