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

            Bug ID: 107754
           Summary: Confusing -Warray-bounds warning with strcpy
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nightstrike at gmail dot com
  Target Milestone: ---

Metabug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=warray-bounds

// Compile with GCC 12.2.0 with -O2:
//    warning: ‘strcpy’ offset 0 is out of the bounds [0, 0] [-Warray-bounds]
// no warning without -O2

struct Inst;
struct Class { int offset; };

static struct Class * classFoo; // No problem without static here

struct Foo
{
   unsigned int a;   // no problem if commented out
   char bar[1024];
};

void setFoo(struct Inst * this, const char * value)
{
   struct Foo * ptr = (struct Foo *)(this ? (((char *)this) + classFoo->offset)
: 0);
   __builtin_strcpy(ptr->bar, value);
}


$ gcc-12 -c -O2 -Warray-bounds a.c -o /dev/null
a.c: In function 'setFoo':
a.c:19:4: warning: '__builtin_strcpy' offset 0 is out of the bounds [0, 0]
[-Warray-bounds]
   19 |    __builtin_strcpy(ptr->bar, value);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to