public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-gcc at vinc17 dot net" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-98447-4-I6vJ71T7SC@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-98447-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #4 from Vincent Lefèvre <vincent-gcc at vinc17 dot net> ---
So this can be simplified to

void f (unsigned long *p, int r, int i)
{
  int b = 64, n = r % 64;

  while (i >= 0 && b >= 0)
    {
      if (b <= n)
        p[i--] = 1UL << b;
      b -= n;
    }
}

Here, due to the "n = r % 64", one has n <= 63, so that "1UL << b" can be
executed only when b <= 63, and the shift is necessarily valid (no overflow).

This could be part of a more general bug: I also get the
-Wanalyzer-shift-count-overflow warning if I replace "n = r % 64" by "n = r".
While with the initial code, one is certain that the shift is valid, now this
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 have
any value.

  parent reply	other threads:[~2020-12-26 18:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-26  0:51 [Bug analyzer/98447] New: " vincent-gcc at vinc17 dot net
2020-12-26 16:35 ` [Bug analyzer/98447] " vincent-gcc at vinc17 dot net
2020-12-26 18:17 ` vincent-gcc at vinc17 dot net
2020-12-26 18:21 ` vincent-gcc at vinc17 dot net
2020-12-26 18:47 ` vincent-gcc at vinc17 dot net [this message]
2021-01-29 13:50 ` vincent-gcc at vinc17 dot net
2021-04-21 11:54 ` vincent-gcc at vinc17 dot net
2023-02-28 12:59 ` vincent-gcc at vinc17 dot net

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-98447-4-I6vJ71T7SC@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).