Skip to contentSkip to conversation

erofs-utils: lib: fix undefined behavior in zstd dict_size bit shift

PATCH Superseded Nithurshen 2 participants 3 messages Feb 22 – Feb 24

Subsystems (2)
lore
Share & tools
lore

Nithurshen

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix undefined behavior in zstd dict_size bit shift

cppcheck static analysis flags that shifting the signed 32-bit literal 1 by ilog2(dict_size) can lead to undefined behavior if the shift amount reaches or exceeds 31.

This patch casts the literal to 1ULL to ensure the shift operates safely on an unsigned 64-bit integer, preventing potential overflows on different architectures.

---
 lib/compressor_libzstd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/compressor_libzstd.c +2 -2
index c475077..f47635e 100644
@@ -123,10 +123,10 @@ static int erofs_compressor_libzstd_setdictsize(struct erofs_compress *c,
 		} else {
 			dict_size = min_t(u32, Z_EROFS_ZSTD_MAX_DICT_SIZE,
 					  pclustersize_max << 3);
-			dict_size = 1 << ilog2(dict_size);
+			dict_size = 1ULL << ilog2(dict_size);
 		}
 	}
-	if (dict_size != 1 << ilog2(dict_size) ||
+	if (dict_size != 1ULL << ilog2(dict_size) ||
 	    dict_size > Z_EROFS_ZSTD_MAX_DICT_SIZE) {
 		erofs_err("invalid dictionary size %u", dict_size);
 		return -EINVAL;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 18 lines · Nithurshen

cppcheck static analysis flags that shifting the signed 32-bit literal 1 by ilog2(dict_size) can lead to undefined behavior if the shift amount reaches or exceeds 31.

This patch casts the literal to 1ULL to ensure the shift operates safely on an unsigned 64-bit integer, preventing potential overflows on different architectures.

---
  lib/compressor_libzstd.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/compressor_libzstd.c +0 -0
index c475077..f47635e 100644

lib/compressor_libzstd.c static int erofs_compressor_libzstd_setdictsize(struct erofs_compress *c, · line 126

Quoted 5 lines
lib/compressor_libzstd.c +1 -1
@@ -123,10 +123,10 @@ static int erofs_compressor_libzstd_setdictsize(struct erofs_compress *c,
  		} else {
  			dict_size = min_t(u32, Z_EROFS_ZSTD_MAX_DICT_SIZE,
  					  pclustersize_max << 3);
-			dict_size = 1 << ilog2(dict_size);
+			dict_size = 1ULL << ilog2(dict_size);

Hi Nithurshen,

Thank you for catching this. I think using '1U' rather than '1ULL' is enough here and below.

Yifan Zhao

Quoted 7 lines
lib/compressor_libzstd.c +1 -1
  		}
  	}
-	if (dict_size != 1 << ilog2(dict_size) ||
+	if (dict_size != 1ULL << ilog2(dict_size) ||
  	    dict_size > Z_EROFS_ZSTD_MAX_DICT_SIZE) {
  		erofs_err("invalid dictionary size %u", dict_size);
  		return -EINVAL;
author
#2 lore raw

PATCH v2 1/1 erofs-utils: lib: fix undefined behavior in zstd dict_size bit shift

cppcheck static analysis flags that shifting the signed 32-bit literal 1 by ilog2(dict_size) can lead to undefined behavior if the shift amount reaches or exceeds 31.

This patch casts the literal to 1U to ensure the shift operates safely on an unsigned 32-bit integer, preventing potential overflows on different architectures.

---
 lib/compressor_libzstd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/compressor_libzstd.c +2 -2
index c475077..6330f44 100644
@@ -123,10 +123,10 @@ static int erofs_compressor_libzstd_setdictsize(struct erofs_compress *c,
 		} else {
 			dict_size = min_t(u32, Z_EROFS_ZSTD_MAX_DICT_SIZE,
 					  pclustersize_max << 3);
-			dict_size = 1 << ilog2(dict_size);
+			dict_size = 1U << ilog2(dict_size);
 		}
 	}
-	if (dict_size != 1 << ilog2(dict_size) ||
+	if (dict_size != 1U << ilog2(dict_size) ||
 	    dict_size > Z_EROFS_ZSTD_MAX_DICT_SIZE) {
 		erofs_err("invalid dictionary size %u", dict_size);
 		return -EINVAL;
Signature · 1 line
2.51.0