From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 116223858401; Sat, 30 Apr 2022 08:24:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 116223858401 From: "bernie at codewiz dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/105438] New: Incorrect array-bounds warning with array size carried over from a previous template instantiation Date: Sat, 30 Apr 2022 08:24:55 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: bernie at codewiz dot 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 bug_severity priority component assigned_to reporter 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: Sat, 30 Apr 2022 08:24:56 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105438 Bug ID: 105438 Summary: Incorrect array-bounds warning with array size carried over from a previous template instantiation Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: bernie at codewiz dot org Target Milestone: --- Minified testcase (almost every line is necessary to reproduce): ``` /* g++ -Warray-bounds -O2 repro.cc */ int longer[7] =3D {}; int shorter[2] =3D {}; int out[10] =3D {}; template void configure(const int(&in)[N], const int nrows =3D N) { if (nrows <=3D 10) { for (int i =3D 0; i < nrows; i++) { out[i] =3D in[i]; } } } int main() { configure(longer); configure(shorter); } ``` Output: ``` $ g++ -Warray-bounds -O2 repro.cc repro.cc: In function 'int main()': repro.cc:13:24: warning: array subscript 'const int [7][0]' is partly outsi= de array bounds of 'int [2]' [-Warray-bounds] 13 | out[i] =3D in[i]; | ~~^ repro.cc:3:5: note: while referencing 'shorter' 3 | int shorter[2] =3D {}; | ^~~~~~~ repro.cc:13:24: warning: array subscript 'const int [7][0]' is partly outsi= de array bounds of 'int [2]' [-Warray-bounds] 13 | out[i] =3D in[i]; | ~~^ repro.cc:3:5: note: while referencing 'shorter' 3 | int shorter[2] =3D {}; | ^~~~~~~ ``` Static analysis appears to be using the length of the longer array for the = call using the shorter array. The warning disappears by: * commenting out the first call to configure() suppresses the warning * swapping the two calls to configure() * commenting out if statement also eliminates the warning * making longer and shorter the same size * using N as loop counter instead of nrows=