public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "nightstrike at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/107754] New: Confusing -Warray-bounds warning with strcpy
Date: Fri, 18 Nov 2022 21:19:50 +0000	[thread overview]
Message-ID: <bug-107754-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2022-11-18 21:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-18 21:19 nightstrike at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107754-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).