public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Potentially false-positive -Wstringop-overflow= warning with gcc >= 11.1
@ 2022-01-28 15:01 Dumitru Ceara
  2022-01-28 15:27 ` Segher Boessenkool
  0 siblings, 1 reply; 14+ messages in thread
From: Dumitru Ceara @ 2022-01-28 15:01 UTC (permalink / raw)
  To: gcc-help

Hi everyone,

I think we've hit a false positive warning report when changing some of
our code and using a relatively new gcc version (11.2).

I tried to simplify the reproducer as much as possible and ended up with
test.c below.  With gcc 10.3 I don't see this warning, while with gcc
11.1 we seem to hit it.

I can open a bug for this but I was wondering if it's maybe already
known/reported.

Thank you!

Regards,
Dumitru

---
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 

$ cat test.c
/* Compile with:
 * # gcc -g -O2 -c -o test.o test.c
 * test.c: In function ‘foo’:
 * test.c:47:5: warning: ‘memcpy’ writing 6 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
 *    47 |     memcpy(h2 + 1, &somedata[0], 6);
 *       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * # gcc -v
 * [...]
 * gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 
 */

#include <string.h>
#include <stdint.h>

static char somedata[1024];

struct pkt {
    void *base_;
    uint16_t l4_ofs;
};

struct hdr1 {
    uint32_t h11;
};

struct hdr2 {
    uint32_t h21;
};

extern void pkt_bar(struct pkt *);

void foo(void)
{
    uint64_t stub[1024 / 8];
    struct pkt p;
 
    p.base_ = &stub[0];
    p.l4_ofs = UINT16_MAX;

    size_t size = 8;
    /* If I comment the next line out the warning goes away. */
    pkt_bar(&p);
    void *data = (char *) p.base_;
    memset(data, 0, size);

    struct hdr1 *h1 = data;
    p.l4_ofs = (uintptr_t)(h1 + 1) - (uintptr_t)p.base_;
 
    void *l4data = p.l4_ofs != UINT16_MAX ? (char *) p.base_ + p.l4_ofs : NULL;
    struct hdr2 *h2 = l4data;

    memcpy(h2 + 1, &somedata[0], 6);
    h2->h21 = 0;
}


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

end of thread, other threads:[~2022-02-02 20:44 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-28 15:01 Potentially false-positive -Wstringop-overflow= warning with gcc >= 11.1 Dumitru Ceara
2022-01-28 15:27 ` Segher Boessenkool
2022-01-28 16:16   ` Dumitru Ceara
2022-01-28 17:37     ` Segher Boessenkool
2022-01-31 21:49     ` Martin Sebor
2022-01-31 22:01       ` Segher Boessenkool
2022-02-01  8:21         ` Jonathan Wakely
2022-02-01  5:18       ` Richard Sandiford
2022-02-01 14:42         ` Segher Boessenkool
2022-02-01 23:54         ` Martin Sebor
2022-02-02 10:12           ` Jonathan Wakely
2022-02-02 18:33             ` Segher Boessenkool
2022-02-02 20:44               ` Jonathan Wakely
2022-02-02 18:23           ` Segher Boessenkool

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).