public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lavr at ncbi dot nlm.nih.gov" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/100395] New: Bogus -Wstringop-overflow warning
Date: Mon, 03 May 2021 14:29:02 +0000	[thread overview]
Message-ID: <bug-100395-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 100395
           Summary: Bogus -Wstringop-overflow warning
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lavr at ncbi dot nlm.nih.gov
  Target Milestone: ---

$ gcc --version
gcc (GCC) 10.2.0

$ cat test.c
#include <stdio.h>
#include <string.h>

int main(int argc, const char* argv[])
{
    size_t len0 = strlen(argv[0]), len1 = strlen(argv[1]);
    char buf[444];
    char* s;

    s = buf + sizeof(buf) - len0;
    memcpy(s, argv[0], len0);
    s -= len1;
    memcpy(--s, argv[1], len1);
    s[len1++] = ' ';
    printf("%.*s\n", (int)(len0 + len1), s);
    return 0;
}

$ gcc -O2 -Wall test.c
test.c: In function ‘main’:
test.c:14:15: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
   14 |     s[len1++] = ' ';
      |     ~~~~~~~~~~^~~~~
test.c:7:10: note: at offset -1 to object ‘buf’ with size 444 declared here
    7 |     char buf[444];
      |          ^~~

The offset shown in the message is wrong, which most likely was the reason to
emit the warning altogether.

The test code above mocks up a situation where the lengths of both strings,
which are being copied in the reverse order into "buf", are known and small,
i.e. both fit (a few times, actually) together into the buffer, including the
separator and terminator.

             reply	other threads:[~2021-05-03 14:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-03 14:29 lavr at ncbi dot nlm.nih.gov [this message]
2021-05-03 16:18 ` [Bug middle-end/100395] " msebor 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-100395-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).