public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94985] New: False-positive -Warray-bounds for char[1] on a non-zero offset in a referenced buffer
@ 2020-05-07 13:33 joeyjyliu at gmail dot com
  2020-05-07 19:26 ` [Bug middle-end/94985] " msebor at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: joeyjyliu at gmail dot com @ 2020-05-07 13:33 UTC (permalink / raw)
  To: gcc-bugs

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)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-07 20:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-07 13:33 [Bug c++/94985] New: False-positive -Warray-bounds for char[1] on a non-zero offset in a referenced buffer joeyjyliu at gmail dot com
2020-05-07 19:26 ` [Bug middle-end/94985] " msebor at gcc dot gnu.org
2020-05-07 19:26 ` msebor at gcc dot gnu.org
2020-05-07 20:23 ` joeyjyliu at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).