https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114430

            Bug ID: 114430
           Summary: False positive for -Wformat
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ad...@tho-otto.de
  Target Milestone: ---

Using GCC 13.2.1, with:


#include <stdio.h>

struct WWPerson
{
    char Name[12];
};

struct WWPerson WWPersons[] = {
    {"Bill R"},
    {"Bill R"},
    {"Barry G"},
};

char item[12 + 1];
void test()
{
    for (size_t i = 0; i < sizeof(WWPersons) / sizeof(struct WWPerson); i++) {
        sprintf(item, "%s", WWPersons[i].Name);
        }
}


When compiling with -O2, i get:

bla.c: In function ‘test’:
bla.c:18:24: warning: ‘%s’ directive writing up to 35 bytes into a region of
size 13 [-Wformat-overflow=]
   18 |         sprintf(item, "%s", WWPersons[i].Name);
      |                        ^~
bla.c:18:9: note: ‘sprintf’ output between 1 and 36 bytes into a destination of
size 13
   18 |         sprintf(item, "%s", WWPersons[i].Name);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I get the same warning with version 10.5.0, but not with version 9.5.0

The warning disappears when i remove one element from the array.

Reply via email to