public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy
@ 2022-11-18 21:19 nightstrike at gmail dot com
  2022-11-18 21:28 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer pinskia at gcc dot gnu.org
  2022-11-18 21:29 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer and non-zero offset for struct array pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: nightstrike at gmail dot com @ 2022-11-18 21:19 UTC (permalink / raw)
  To: gcc-bugs

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);
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-18 21:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-18 21:19 [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy nightstrike at gmail dot com
2022-11-18 21:28 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer pinskia at gcc dot gnu.org
2022-11-18 21:29 ` [Bug tree-optimization/107754] Confusing -Warray-bounds warning with strcpy with a null pointer and non-zero offset for struct array pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).