public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/96900] New: bogus -Warray-bounds on strlen with valid pointer obtained from just-past-the-end
Date: Wed, 02 Sep 2020 16:50:41 +0000	[thread overview]
Message-ID: <bug-96900-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 96900
           Summary: bogus -Warray-bounds on strlen with valid pointer
                    obtained from just-past-the-end
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

When a valid pointer into an array that has been derived from a past-the-end
pointer to a member array of an initialized constant struct is used in a call
to a string built-in like strlen GCC issues a bogus -Warray-bounds warning
indicating that the offset into the array is out of its bounds.

$ cat q.c && gcc -S -Wall q.c
struct S { char n, a[3]; };

const char a[3] = { 2, 1, 0 };
const struct S s = { 3, { 2, 1, 0 } };

int f (void)
{
  const char *p = &a[sizeof a];
  return __builtin_strlen (p - sizeof a);      // no warning (good)
}

int g (void)
{
  const char *p = &s.a[sizeof s.a];
  return __builtin_strlen (p - sizeof s.a);    // bogus -Warray-bounds
}

q.c: In function ‘g’:
q.c:15:10: warning: offset ‘1’ outside bounds of constant string
[-Warray-bounds]
   15 |   return __builtin_strlen (p - sizeof s.a);    // bogus -Warray-bounds
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
q.c:4:16: note: ‘s’ declared here
    4 | const struct S s = { 3, { 2, 1, 0 } };
      |                ^

             reply	other threads:[~2020-09-02 16:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-02 16:50 msebor at gcc dot gnu.org [this message]
2020-09-02 16:51 ` [Bug middle-end/96900] [9/10/11 Regression] " msebor at gcc dot gnu.org
2020-09-02 18:57 ` msebor at gcc dot gnu.org
2020-09-03  6:15 ` rguenth at gcc dot gnu.org
2020-09-03 23:37 ` msebor at gcc dot gnu.org
2021-01-14  9:19 ` rguenth at gcc dot gnu.org
2021-02-14  0:26 ` msebor at gcc dot gnu.org
2021-06-01  8:18 ` [Bug middle-end/96900] [9/10/11/12 " rguenth at gcc dot gnu.org
2022-05-27  9:43 ` [Bug middle-end/96900] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-28 10:41 ` jakub at gcc dot gnu.org
2023-07-07 10:38 ` [Bug middle-end/96900] [11/12/13/14 " rguenth 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-96900-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).