From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 0AD37385842B; Mon, 7 Mar 2022 18:34:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0AD37385842B From: "msebor at gcc dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: msebor at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 12.0 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Mar 2022 18:34:24 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104789 --- Comment #9 from Martin Sebor --- 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 =3D 0; for (unsigned j =3D 0; i !=3D n; ++j) i +=3D 2; if (i > 7) return; while (i % 4) a[i++] =3D 0; } z.c: In function =E2=80=98f=E2=80=99: z.c:14:12: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=3D] 14 | a[i++] =3D 0; | ~~~~~~~^~~ z.c:1:6: note: at offset 8 into destination object =E2=80=98a=E2=80=99 of s= ize 8 1 | char a[8]; | ^ The warning can be avoided (and the emitted object code improved) by changi= ng the while loop like so: while (i % 4) { if (i > 7) __builtin_unreachable ();=20 a[i++] =3D 0; } The same suppression works in the test case in comment #7 but GCC then issu= es 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 =E2=80=98void std::__new_allocator<_Tp>::construct(_Up*,= _Args&& ...) [with _Up =3D unsigned char; _Args =3D {const unsigned char&}; _Tp =3D= unsigned char]=E2=80=99, inlined from =E2=80=98static void std::allocator_traits >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up =3D unsigned cha= r; _Args =3D {const unsigned char&}; _Tp =3D unsigned char]=E2=80=99 at /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/alloc_trait= s.h:516:17, inlined from =E2=80=98void std::vector<_Tp, _Alloc>::push_back(const va= lue_type&) [with _Tp =3D unsigned char; _Alloc =3D std::allocator]=E2= =80=99 at /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/stl_vector.= h:1280:30, inlined from =E2=80=98void commit_temp_packets()=E2=80=99 at pr104789.C= :37:17: /build/gcc-master/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/new_allocat= or.h:175:11: warning: =E2=80=98header=E2=80=99 may be used uninitialized [-Wmaybe-uninit= ialized] 175 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pr104789.C: In function =E2=80=98void commit_temp_packets()=E2=80=99: pr104789.C:10:17: note: =E2=80=98header=E2=80=99 declared here 10 | uint8_t header[8]; | ^~~~~~=