Skip to contentSkip to conversation

parse: shifting by full number of bits is undefined

PATCH In review Jason A. Donenfeld 2 participants 2 messages Oct 26, 2018 1 message not in the archive Sparse checker

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CAHmME9oZk7Afns=W=xM=6waqvVfV+iAdAcB6731xzUUcm4P0zA@mail.gmail.com

#1 lore raw

PATCH 1/1 parse: shifting by full number of bits is undefined

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.

---
 parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
parse.c +1 -1
index 02a55a7..02d0615 100644
@@ -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;
Signature · 1 line
2.19.1
#2 lore raw
Quoted 7 lines · Jason A. Donenfeld

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)

Hi Jason,

This is already fixed in the development tree:
git://github.com/lucvoo/sparse.git
together with some other fixes & changes regarding enums.

I hope to be able to push this to the official tree real soon now.

Kind regards,
-- Luc