public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/104965] New: Yet another -Warray-bounds false positive
@ 2022-03-17 12:15 redi at gcc dot gnu.org
  2022-03-17 12:52 ` [Bug middle-end/104965] [11/12 Regression] " rguenth at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-03-17 12:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

end of thread, other threads:[~2023-05-29 10:06 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-17 12:15 [Bug middle-end/104965] New: Yet another -Warray-bounds false positive redi at gcc dot gnu.org
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

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