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/101397] New: spurious warning writing to the result of stpcpy minus 1
Date: Fri, 09 Jul 2021 18:23:08 +0000	[thread overview]
Message-ID: <bug-101397-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 101397
           Summary: spurious warning writing to the result of stpcpy minus
                    1
           Product: gcc
           Version: 11.1.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: ---

This is reduced from a recent Glibc build with GCC 12 which shows the warning
below:

In function ‘nis_local_group’,
    inlined from ‘nis_local_group’ at nis_local_names.c:27:1:
nis_local_names.c:38:13: error: array subscript -1 is outside array bounds of
‘char[1025]’ [-Werror=array-bounds]
   38 |       if (cp[-1] != '.')
      |           ~~^~~~
nis_local_names.c: In function ‘nis_local_group’:
nis_local_names.c:29:15: note: at offset -1 into object ‘__nisgroup’ of size
1025
   29 |   static char __nisgroup[NIS_MAXNAMELEN + 1];
      |               ^~~~~~~~~~

The following test case shows the warning is a false positive.  Since stpcpy()
returns a pointer to the terminating null it appends to the destination neither
of the warnings below is appropriate since there's no indication that the
copied string is empty.  The output below is with GCC 11.1.  In GCC 12 the
second -Wstringop-overflow becomes a -Warray-bounds.

$ cat t.c && gcc -O2 -S -Wall t.c
void f (void*);

void g (const char *s)
{
  char d[8];
  char *t = __builtin_stpcpy (d, s);
  __builtin_strcpy (t - 1, "x");
  f (d);
}

void h (const char *s)
{
  char d[8];
  char *t = __builtin_stpcpy (d, s);
  t[-1] = 0;
  f (d);
}

t.c: In function ‘g’:
t.c:7:3: warning: ‘__builtin_memcpy’ writing 2 bytes into a region of size 0
overflows the destination [-Wstringop-overflow=]
    7 |   __builtin_strcpy (t - 1, "x");
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
t.c:5:8: note: at offset -1 into destination object ‘d’ of size 8
    5 |   char d[8];
      |        ^
t.c: In function ‘h’:
t.c:15:9: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
   15 |   t[-1] = 0;
      |   ~~~~~~^~~
t.c:13:8: note: at offset -1 into destination object ‘d’ of size 8
   13 |   char d[8];
      |        ^

             reply	other threads:[~2021-07-09 18:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09 18:23 msebor at gcc dot gnu.org [this message]
2021-07-09 18:25 ` [Bug middle-end/101397] [11/12 Regression] " msebor at gcc dot gnu.org
2021-07-12 17:20 ` msebor at gcc dot gnu.org
2021-07-15  1:50 ` msebor at gcc dot gnu.org
2021-07-20 19:52 ` cvs-commit at gcc dot gnu.org
2021-07-20 19:53 ` [Bug middle-end/101397] [11 " msebor at gcc dot gnu.org
2021-07-28  7:07 ` rguenth at gcc dot gnu.org
2021-10-26 14:24 ` siddhesh at gotplt dot org
2021-11-09  0:06 ` msebor at gcc dot gnu.org
2021-12-09 16:58 ` msebor at gcc dot gnu.org
2022-04-21  7:49 ` rguenth at gcc dot gnu.org
2023-05-29 10:05 ` jakub 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-101397-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).