public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/104965] New: Yet another -Warray-bounds false positive
Date: Thu, 17 Mar 2022 12:15:02 +0000	[thread overview]
Message-ID: <bug-104965-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 104965
           Summary: Yet another -Warray-bounds false positive
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
            Blocks: 56456
  Target Milestone: ---

Maybe another dup, I can't keep track.

#include <string>

template<typename T>
T* f(const std::basic_string<T>& str)
{
  auto n = str.size();
  auto p = new T[n];
  str.copy(p, n);
  return p;
}

int main()
{
  std::basic_string<unsigned short> s;
  auto p = f(s);
  char c = 0;
  if (s.size())
    c = *p;
  delete[] p;
  return c;
}


With -O2

copy.cc: In function 'int main()':
copy.cc:18:9: warning: array subscript 0 is outside array bounds of 'short
unsigned int [0]' [-Warray-bounds]
   18 |     c = *p;
      |         ^~
In function 'T* f(const std::__cxx11::basic_string<_CharT>&) [with T = short
unsigned int]',
    inlined from 'int main()' at copy.cc:15:13:
copy.cc:7:12: note: object of size 0 allocated by 'operator new []'
    7 |   auto p = new T[n];
      |            ^~~~~~~~


This is ridiculous. The array subscript is guarded by the same length as the
array. GCC manages to use the string length to determine the allocation size,
but can't use it to confirm the conditional read doesn't happen.


Referenced Bugs:

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

             reply	other threads:[~2022-03-17 12:15 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 12:15 redi at gcc dot gnu.org [this message]
2022-03-17 12:52 ` [Bug middle-end/104965] [11/12 Regression] " rguenth at gcc dot gnu.org
2022-03-17 13:47 ` redi at gcc dot gnu.org
2022-03-17 16:00 ` msebor at gcc dot gnu.org
2022-03-17 23:46 ` pinskia at gcc dot gnu.org
2022-04-21  7:51 ` rguenth at gcc dot gnu.org
2022-07-26 12:41 ` [Bug middle-end/104965] [11/12/13 " rguenth at gcc dot gnu.org
2022-07-26 12:41 ` rguenth at gcc dot gnu.org
2022-07-26 13:59 ` redi at gcc dot gnu.org
2023-05-29 10:06 ` [Bug middle-end/104965] [11/12/13/14 " 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-104965-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).