linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: linux-sparse@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>
Subject: [PATCH sparse] parse: shifting by full number of bits is undefined
Date: Fri, 26 Oct 2018 05:17:00 +0200	[thread overview]
Message-ID: <20181026031700.12310-1-Jason@zx2c4.com> (raw)
In-Reply-To: <CAHmME9oZk7Afns=W=xM=6waqvVfV+iAdAcB6731xzUUcm4P0zA@mail.gmail.com>

The type checker wasn't identifying upper bounds for huge unsigned
64-bit numbers, because the right shift turned into a no-op:

zx2c4@thinkpad /tmp $ cat sparse.c
enum { sparse_does_not_like_this = 0x8000000000000003ULL };
zx2c4@thinkpad /tmp $ sparse sparse.c
sparse.c:1:36: warning: cast truncates bits from constant value (8000000000000003 becomes 3)

This works around the issue by detecting when we're going to shift by
the size of the variable and treat that as always zero.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/parse.c b/parse.c
index 02a55a7..02d0615 100644
--- a/parse.c
+++ b/parse.c
@@ -788,7 +788,7 @@ static int type_is_ok(struct symbol *type, Num *upper, Num *lower)
 
 	if (!is_unsigned)
 		shift--;
-	if (upper->x == 0 && upper->y >> shift)
+	if (upper->x == 0 && (shift < (sizeof(upper->y) << 3)) && upper->y >> shift)
 		return 0;
 	if (lower->x == 0 || (!is_unsigned && (~lower->y >> shift) == 0))
 		return 1;
-- 
2.19.1


       reply	other threads:[~2018-10-26  3:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAHmME9oZk7Afns=W=xM=6waqvVfV+iAdAcB6731xzUUcm4P0zA@mail.gmail.com>
2018-10-26  3:17 ` Jason A. Donenfeld [this message]
2018-10-26  9:01   ` [PATCH sparse] parse: shifting by full number of bits is undefined Luc Van Oostenryck

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=20181026031700.12310-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sparse@vger.kernel.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 NNTP newsgroup(s).