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

            Bug ID: 94985
           Summary: False-positive -Warray-bounds for char[1] on a
                    non-zero offset in a referenced buffer
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joeyjyliu at gmail dot com
  Target Milestone: ---

Created attachment 48474
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48474&action=edit
patch for -Warray-bounds with test case

I think the assumption in builtin_memref::set_base_and_offset
(gimple-ssa-warn-restrict.c) is not necessarily correct. It is only applicable
for flexible arrays. Actually even for flexible arrays this check is
controversial as it assumes the pointer to the struct is always a pointer to
the array of structs.  

In the attachment, there is a test file. The incorrect warning is like below.

error: ‘char* strncpy(char*, const char*, size_t)’ offset 1 from the object at
‘<unknown>’ is out of the bounds of referenced subobject ‘cstr<1>::m_data’ with
type ‘char [1]’ at offset 1 [-Werror=array-bounds]
    8 |         ::strncpy( data(), src, n );
      |         ~~~~~~~~~^~~~~~~~~~~~~~~~~~

Also in the attachment there is a suggested change. With that, the warning goes
away and the regression tests still pass. 

The original patch was sent to gcc-patches but I was advised to open a Bugzilla
ticket first. So here it is and the patch is modified to accommodate the
existing regression tests. 

$ g++ -v
Using built-in specs.
COLLECT_GCC=/local/usr/bin/g++
COLLECT_LTO_WRAPPER=/local/usr/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure --prefix=/local/usr --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.0.0 20200505 (experimental) (GCC)

Reply via email to