From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E8420385801E; Thu, 17 Mar 2022 12:15:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8420385801E From: "redi at gcc dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter blocked target_milestone Message-ID: 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: Thu, 17 Mar 2022 12:15:03 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D104965 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 template T* f(const std::basic_string& str) { auto n =3D str.size(); auto p =3D new T[n]; str.copy(p, n); return p; } int main() { std::basic_string s; auto p =3D f(s); char c =3D 0; if (s.size()) c =3D *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 =3D *p; | ^~ In function 'T* f(const std::__cxx11::basic_string<_CharT>&) [with T =3D sh= ort 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 =3D 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 siz= e, but can't use it to confirm the conditional read doesn't happen. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D56456 [Bug 56456] [meta-bug] bogus/missing -Warray-bounds=