public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/104789] [12 Regression] -Wstringop-overflow false positive at -O3 for an unrolled loop
Date: Mon, 07 Mar 2022 18:34:23 +0000	[thread overview]
Message-ID: <bug-104789-4-BteFJiXrD6@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-104789-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #9 from Martin Sebor <msebor at gcc dot gnu.org> ---
A much simplified test case that reproduces the same warning (with both GCC 12
and 11) is below.  The underlying problem is that although GCC does have a way
to represent simple disjoint ranges of variable values, it's not used here, and
even if it were, very little code makes uses of this representation beyond a
single contiguous range (or, at most, a converse of it).  Even the disjoint
representation isn't sufficiently flexible to capture arbitrarily complex
ranges (capturing those in their full generality would require a constraint
solver).

$ cat z.c && gcc -O3 -S -Wall z.c
char a[8];

void f (unsigned n)
{
  unsigned i = 0;

  for (unsigned j = 0; i != n; ++j)
    i += 2;

  if (i > 7)
    return;

  while (i % 4)
    a[i++] = 0;
}
z.c: In function ‘f’:
z.c:14:12: warning: writing 1 byte into a region of size 0
[-Wstringop-overflow=]
   14 |     a[i++] = 0;
      |     ~~~~~~~^~~
z.c:1:6: note: at offset 8 into destination object ‘a’ of size 8
    1 | char a[8];
      |      ^

The warning can be avoided (and the emitted object code improved) by changing
the while loop like so:

  while (i % 4)
    {
      if (i > 7) __builtin_unreachable (); 
      a[i++] = 0;
    }

The same suppression works in the test case in comment #7 but GCC then issues
another warning, this one pointing out that an element of the header array is
used uninitialized.  That warning looks valid to me:

/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/allocator.h:46,
                 from
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/vector:61,
                 from pr104789.C:2:
In member function ‘void std::__new_allocator<_Tp>::construct(_Up*, _Args&&
...) [with _Up = unsigned char; _Args = {const unsigned char&}; _Tp = unsigned
char]’,
    inlined from ‘static void std::allocator_traits<std::allocator<_Tp1>
>::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = unsigned char;
_Args = {const unsigned char&}; _Tp = unsigned char]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/alloc_traits.h:516:17,
    inlined from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&)
[with _Tp = unsigned char; _Alloc = std::allocator<unsigned char>]’ at
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.h:1280:30,
    inlined from ‘void commit_temp_packets()’ at pr104789.C:37:17:
/build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/new_allocator.h:175:11:
warning: ‘header’ may be used uninitialized [-Wmaybe-uninitialized]
  175 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pr104789.C: In function ‘void commit_temp_packets()’:
pr104789.C:10:17: note: ‘header’ declared here
   10 |         uint8_t header[8];
      |                 ^~~~~~

  parent reply	other threads:[~2022-03-07 18:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 16:26 [Bug tree-optimization/104789] New: [12 Regression] New -Wstringop-overflow false positive since r12-5863-g9354a7d70caef1c9 marxin at gcc dot gnu.org
2022-03-04 16:26 ` [Bug tree-optimization/104789] " marxin at gcc dot gnu.org
2022-03-04 16:27 ` marxin at gcc dot gnu.org
2022-03-04 19:54 ` pinskia at gcc dot gnu.org
2022-03-04 19:57 ` pinskia at gcc dot gnu.org
2022-03-07 11:05 ` rverschelde at gmail dot com
2022-03-07 12:05 ` pinskia at gcc dot gnu.org
2022-03-07 12:39 ` rverschelde at gmail dot com
2022-03-07 13:30 ` rverschelde at gmail dot com
2022-03-07 18:10 ` msebor at gcc dot gnu.org
2022-03-07 18:34 ` msebor at gcc dot gnu.org [this message]
2022-03-09  9:45 ` [Bug tree-optimization/104789] [12 Regression] -Wstringop-overflow false positive at -O3 for an unrolled loop rguenth at gcc dot gnu.org
2022-03-14 17:35 ` msebor at gcc dot gnu.org
2022-05-06  8:32 ` [Bug tree-optimization/104789] [12/13 " jakub at gcc dot gnu.org
2022-07-26 12:43 ` rguenth at gcc dot gnu.org
2023-05-08 12:24 ` [Bug tree-optimization/104789] [12/13/14 " rguenth 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-104789-4-BteFJiXrD6@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).