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

            Bug ID: 105726
           Summary: spurious warning with -Warray-bounds
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cuzdav at gmail dot com
  Target Milestone: ---

Created attachment 53030
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53030&action=edit
preprocessed version

I just ran into an issue that seems to have appeared in Gcc 10.1 and is still
there in 11, 12, and in the trunk.

The following code produces a spurious warning about reading out of bounds,
but if I remove the "pad" field, or change from std::array to a "raw" array,
the warning goes away.  

//===============================
#include <array>
#include <cstring>

struct X {
    char pad[4];
    std::array<char, 1> mField;
};

void encode(char* aBuffer, const X& aMessage) {
    strncpy(aBuffer, aMessage.mField.data(), 1);
}
//===============================

LIVE: https://godbolt.org/z/zEsx5P8eP

<source>: In function 'void encode(char*, const X&)':
<source>:10:16: warning: 'char* strncpy(char*, const char*, size_t)' offset 4
from the object at 'aMessage' is out of the bounds of referenced subobject
'std::array<char, 1>::_M_elems' with type 'char [1]' at offset 4
[-Warray-bounds]
   10 |         strncpy(aBuffer, aMessage.mField.data(), 1);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from <source>:1:
/opt/compiler-explorer/gcc-12.1.0/include/c++/12.1.0/array:115:56: note:
subobject 'std::array<char, 1>::_M_elems' declared here
  115 |       typename _AT_Type::_Type                         _M_elems;
      |                                                        ^~~~~~~~
Compiler returned: 0


OUTPUT of g++ -v

Using built-in specs.
COLLECT_GCC=/opt/imc/gcc-12.1.0/bin/g++
COLLECT_LTO_WRAPPER=/opt/imc/gcc-12.1.0/libexec/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-12.1.0/configure --prefix=/opt/imc/gcc-12.1.0
--enable-languages=c,c++,fortran,lto --disable-multilib
--with-build-time-tools=/build/INSTALLDIR//opt/imc/gcc-12.1.0/bin
--enable-libstdcxx-time=rt
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.1.0 (GCC)

Reply via email to