public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109727] New: [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined
@ 2023-05-04  6:47 dani at danielbertalan dot dev
  2023-05-04  7:21 ` [Bug middle-end/109727] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: dani at danielbertalan dot dev @ 2023-05-04  6:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109727
           Summary: [13/14 Regression] -Warray-bounds false positive with
                    -fsanitize=undefined
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dani at danielbertalan dot dev
  Target Milestone: ---

The following code results in a -Warray-bounds warning with GCC 13.1+ and
-fsanitize=undefined. No diagnostics are produced by older compilers or if
UBSan is not enabled.

template <unsigned inline_capacity> struct ByteBuffer {
  static ByteBuffer create_uninitialized();
  static void copy() {
      auto new_buf = create_uninitialized();
      new_buf.data();
  }
  char *data() { return m_inline ? m_inline_buffer : m_outline_buffer; }
  union {
    char m_inline_buffer[inline_capacity];
    char *m_outline_buffer;
  };
  bool m_inline;
};

void test()  {
  ByteBuffer<56> buf1;
  buf1.data();
  ByteBuffer<2>::copy();
}

Compile with g++ -O2 -Warray-bounds -fsanitize=undefined:

In member function 'char* ByteBuffer<inline_capacity>::data() [with unsigned
int inline_capacity = 56]',
    inlined from 'char* ByteBuffer<inline_capacity>::data() [with unsigned int
inline_capacity = 2]' at <source>:7:9,
    inlined from 'static void ByteBuffer<inline_capacity>::copy() [with
unsigned int inline_capacity = 2]' at <source>:5:19,
    inlined from 'void test()' at <source>:18:22:
<source>:7:34: warning: array subscript 'ByteBuffer<56>[0]' is partly outside
array bounds of 'ByteBuffer<2> [1]' [-Warray-bounds=]
    7 |   char *data() { return m_inline ? m_inline_buffer : m_outline_buffer;
}
      |                         ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>: In function 'void test()':
<source>:4:12: note: object 'new_buf' of size 16
    4 |       auto new_buf = create_uninitialized();
      |            ^~~~~~~

It looks like something about the two SSO buffer lengths gets confused: the
error trace incorrectly indicates that ByteBuffer<56>::data() is getting
inlined into ByteBuffer<2>::data(). Removing either this template parameter,
the m_outline_buffer union member, or calling ByteBuffer::copy with the same
template parameter in test() fixes the diagnostic.

https://godbolt.org/z/6rqEnhP6q

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

* [Bug middle-end/109727] [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined
  2023-05-04  6:47 [Bug c++/109727] New: [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined dani at danielbertalan dot dev
@ 2023-05-04  7:21 ` rguenth at gcc dot gnu.org
  2023-07-27  9:26 ` rguenth at gcc dot gnu.org
  2024-03-08 15:37 ` law at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-04  7:21 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |56456
   Target Milestone|---                         |13.2


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
[Bug 56456] [meta-bug] bogus/missing -Warray-bounds

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

* [Bug middle-end/109727] [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined
  2023-05-04  6:47 [Bug c++/109727] New: [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined dani at danielbertalan dot dev
  2023-05-04  7:21 ` [Bug middle-end/109727] " rguenth at gcc dot gnu.org
@ 2023-07-27  9:26 ` rguenth at gcc dot gnu.org
  2024-03-08 15:37 ` law at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-27  9:26 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.2                        |13.3

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 13.2 is being released, retargeting bugs to GCC 13.3.

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

* [Bug middle-end/109727] [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined
  2023-05-04  6:47 [Bug c++/109727] New: [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined dani at danielbertalan dot dev
  2023-05-04  7:21 ` [Bug middle-end/109727] " rguenth at gcc dot gnu.org
  2023-07-27  9:26 ` rguenth at gcc dot gnu.org
@ 2024-03-08 15:37 ` law at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-08 15:37 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |law at gcc dot gnu.org
           Priority|P3                          |P2

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

end of thread, other threads:[~2024-03-08 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-04  6:47 [Bug c++/109727] New: [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined dani at danielbertalan dot dev
2023-05-04  7:21 ` [Bug middle-end/109727] " rguenth at gcc dot gnu.org
2023-07-27  9:26 ` rguenth at gcc dot gnu.org
2024-03-08 15:37 ` law at gcc dot gnu.org

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).