[Bug c++/94985] New: False-positive -Warray-bounds for char[1] on a non-zero offset in a referenced buffer

2020-05-07 Thread joeyjyliu at gmail dot com
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
‘’ 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)

[Bug middle-end/94985] False-positive -Warray-bounds for char[1] on a non-zero offset in a referenced buffer

2020-05-07 Thread joeyjyliu at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94985

--- Comment #2 from Joey Liu  ---
Just fyi, the patch attached in this ticket is slightly different than the
patch in the mailing list (I limit it to flexible array only). It can handle
the existing test cases (no regressions). However I do think we can do better
than that since it still can't deduce the correct refsize for my test case. For
now tree::component_ref_size just returns NULL_TREE as it can't really
distinguish the regular a[1] and the flexible array a[1].