Skip to contentSkip to conversation

erofs-utils: fix undefined behavior in erofs_init_devices()

PATCH New Nithurshen 1 participant 1 message Mar 5 Library code

lore
Share & tools
lore

Nithurshen

author
#0 lore raw

PATCH 1/1 erofs-utils: fix undefined behavior in erofs_init_devices()

Using roundup_pow_of_two() on (ondisk_extradevs + 1) triggers a potential 64-bit shift warning because of the internal n-1 logic in the macro.

Since ondisk_extradevs is already validated to be non-zero, use fls_long() directly to calculate the device_id_mask. This resolves the 'shiftTooManyBits' warning and is mathematically equivalent.

---
 lib/super.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/super.c +3 -2
index 088c9a0..bff9b1e 100644
@@ -29,7 +29,8 @@ static bool check_layout_compatibility(struct erofs_sb_info *sbi,
 static int erofs_init_devices(struct erofs_sb_info *sbi,
 			      struct erofs_super_block *dsb)
 {
-	unsigned int ondisk_extradevs, i;
+	unsigned int i;
+	u16 ondisk_extradevs;
 	erofs_off_t pos;
 
 	sbi->total_blocks = sbi->primarydevice_blocks;
@@ -49,7 +50,7 @@ static int erofs_init_devices(struct erofs_sb_info *sbi,
 		return 0;
 
 	sbi->extra_devices = ondisk_extradevs;
-	sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
+	sbi->device_id_mask = (1UL << fls_long(ondisk_extradevs)) - 1;
 	sbi->devs = calloc(ondisk_extradevs, sizeof(*sbi->devs));
 	if (!sbi->devs)
 		return -ENOMEM;
Signature · 1 line
2.51.0