From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4054D385701F; Sat, 26 Dec 2020 18:47:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4054D385701F From: "vincent-gcc at vinc17 dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/98447] incorrect -Wanalyzer-shift-count-overflow warning Date: Sat, 26 Dec 2020 18:47:55 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: vincent-gcc at vinc17 dot net X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- 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: Sat, 26 Dec 2020 18:47:55 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D98447 --- Comment #4 from Vincent Lef=C3=A8vre --- So this can be simplified to void f (unsigned long *p, int r, int i) { int b =3D 64, n =3D r % 64; while (i >=3D 0 && b >=3D 0) { if (b <=3D n) p[i--] =3D 1UL << b; b -=3D n; } } Here, due to the "n =3D r % 64", one has n <=3D 63, so that "1UL << b" can = be executed only when b <=3D 63, and the shift is necessarily valid (no overfl= ow). This could be part of a more general bug: I also get the -Wanalyzer-shift-count-overflow warning if I replace "n =3D r % 64" by "n = =3D r". While with the initial code, one is certain that the shift is valid, now th= is depends on the value of r. But since the compiler doesn't know the condition under which this function will be called, it should not assume that r may h= ave any value.=