public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dani at danielbertalan dot dev" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/109727] New: [13/14 Regression] -Warray-bounds false positive with -fsanitize=undefined
Date: Thu, 04 May 2023 06:47:02 +0000	[thread overview]
Message-ID: <bug-109727-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2023-05-04  6:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-04  6:47 dani at danielbertalan dot dev [this message]
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
2024-05-21  9:14 ` [Bug middle-end/109727] [13/14/15 " jakub at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109727-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).