Skip to contentSkip to conversation

linux/bitmap.h: fix potential sign-extension overflow

PATCH In review Gustavo A. R. Silva 2 participants 2 messages Oct 15, 2019 Bitmap API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux/bitmap.h: fix potential sign-extension overflow

In expression 0xff << offset, left shifting by more than 31 bits has undefined behavior. Notice that the shift amount, *offset*, can be as much as 63.

Fix this by adding suffix ULL to integer 0xFF.

Addresses-Coverity: 1487071 ("Bad bit shift operation")

Fixes d33f5cbaadd8 ("bitops: introduce the for_each_set_clump8 macro")
---
 include/linux/bitmap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bitmap.h +1 -1
index 942871bfe47e..96f91db25b06 100644
@@ -520,7 +520,7 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
 	const size_t index = BIT_WORD(start);
 	const unsigned long offset = start % BITS_PER_LONG;
 
-	map[index] &= ~(0xFF << offset);
+	map[index] &= ~(0xFFULL << offset);
 	map[index] |= value << offset;
 }
 
Signature · 1 line
2.23.0
#1 lore raw
Quoted 5 lines · Gustavo A. R. Silva

In expression 0xff << offset, left shifting by more than 31 bits has undefined behavior. Notice that the shift amount, *offset*, can be as much as 63.

Fix this by adding suffix ULL to integer 0xFF.

[]

include/linux/bitmap.h

Quoted 1 line
include/linux/bitmap.h +0 -0

[]

include/linux/bitmap.h static inline void bitmap_set_value8(unsigned long *map, unsigned long value, · line 523

Quoted 6 lines
include/linux/bitmap.h +1 -1
@@ -520,7 +520,7 @@ static inline void bitmap_set_value8(unsigned long *map, unsigned long value,
 	const size_t index = BIT_WORD(start);
 	const unsigned long offset = start % BITS_PER_LONG;
 
-	map[index] &= ~(0xFF << offset);
+	map[index] &= ~(0xFFULL << offset);

BITS_PER_LONG is 32 and 0xFFULL is 64 bit
when compiled for 32 bit arches.

This should just be 0xFFUL and not ULL.