Bug 118212 - False positive -Wstringop-truncation despite [[gnu::nonstring]]
Summary: False positive -Wstringop-truncation despite [[gnu::nonstring]]
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 14.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, false-positive
Depends on:
Blocks: Wstringop-truncation
  Show dependency treegraph
 
Reported: 2024-12-27 02:54 UTC by Luke Shumaker
Modified: 2024-12-27 03:26 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Luke Shumaker 2024-12-27 02:54:52 UTC
System / GCC version / GCC options:

    $ gcc -v
    Using built-in specs.
    COLLECT_GCC=/usr/bin/gcc
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/lto-wrapper
    Target: x86_64-pc-linux-gnu
    Configured with: /build/gcc/src/gcc/configure --enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust --enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues --with-build-config=bootstrap-lto --with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit --enable-cet=auto --enable-checking=release --enable-clocale=gnu --enable-default-pie --enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object --enable-libstdcxx-backtrace --enable-link-serialization=1 --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin --enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch --disable-werror
    Thread model: posix
    Supported LTO compression algorithms: zlib zstd
    gcc version 14.2.1 20240910 (GCC)

Reproducer source code:

    static struct {
      [[gnu::nonstring]] char name[16];
    } table[1] = {0};

    void set_name(unsigned int rownum, const char *name) {
      __builtin_strncpy(table[rownum].name, name, sizeof(table[0].name));
    }

Reproducer command and output:

    $ gcc -O2 -Wstringop-truncation -Werror -c f.c
    f.c: In function ‘set_name’:
    f.c:6:3: error: ‘__builtin_strncpy’ specified bound 16 equals destination size [-Werror=stringop-truncation]
        6 |   __builtin_strncpy(table[rownum].name, name, sizeof(table[0].name));
          |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    cc1: all warnings being treated as errors

Notes:

    This goes away if the size of table is increased beyond `1`, but
    comes back if rownum is set to the table size minus 1:

        static struct {
          [[gnu::nonstring]] char name[16];
        } table[3] = {0};
        
        void set_name(const char *name) {
          unsigned int rownum = 3;
          __builtin_strncpy(table[rownum-1].name, name, sizeof(table[0].name));
        }