Potentially false-positive -Wstringop-overflow= warning with gcc >= 11.1

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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;
}




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux