public inbox for linux-erofs@ozlabs.org
 help / color / mirror / Atom feed
From: Nithurshen <nithurshen.dev@gmail.com>
To: linux-erofs@lists.ozlabs.org
Cc: xiang@kernel.org, hsiangkao@linux.alibaba.com,
	Nithurshen <nithurshen.dev@gmail.com>
Subject: [PATCH 2/2] erofs-utils: libzstd: fix undefined behavior shift in setdictsize
Date: Sun, 19 Apr 2026 18:46:04 +0530	[thread overview]
Message-ID: <20260419131604.95875-2-nithurshen.dev@gmail.com> (raw)
In-Reply-To: <20260419131604.95875-1-nithurshen.dev@gmail.com>

In erofs_compressor_libzstd_setdictsize(), if pclustersize_max is 0,
dict_size becomes 0, leading to undefined behavior when calling
ilog2(0). This results in an invalid bit shift (e.g., shifting
a 32-bit value by 63 bits), as reported by cppcheck.

Fix this by adding guards to ensure dict_size is non-zero before
performing power-of-two rounding and validation.

Signed-off-by: Nithurshen <nithurshen.dev@gmail.com>
---
 lib/compressor_libzstd.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/compressor_libzstd.c b/lib/compressor_libzstd.c
index 6330f44..eb768de 100644
--- a/lib/compressor_libzstd.c
+++ b/lib/compressor_libzstd.c
@@ -123,10 +123,11 @@ 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 = 1U << ilog2(dict_size);
+			if (dict_size)
+				dict_size = 1U << ilog2(dict_size);
 		}
 	}
-	if (dict_size != 1U << ilog2(dict_size) ||
+	if (!dict_size || dict_size != 1U << ilog2(dict_size) ||
 	    dict_size > Z_EROFS_ZSTD_MAX_DICT_SIZE) {
 		erofs_err("invalid dictionary size %u", dict_size);
 		return -EINVAL;
-- 
2.52.0



  reply	other threads:[~2026-04-19 13:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-19 13:16 [PATCH 1/2] erofs-utils: fix undefined behavior shift in erofs_init_devices Nithurshen
2026-04-19 13:16 ` Nithurshen [this message]
2026-04-20  1:54   ` [PATCH 2/2] erofs-utils: libzstd: fix undefined behavior shift in setdictsize Gao Xiang
2026-04-20  1:52 ` [PATCH 1/2] erofs-utils: fix undefined behavior shift in erofs_init_devices Gao Xiang
2026-04-20  2:50   ` [PATCH 2/2] erofs-utils: libzstd: fix undefined behavior shift in setdictsize Nithurshen
2026-04-20  2:57     ` Gao Xiang

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=20260419131604.95875-2-nithurshen.dev@gmail.com \
    --to=nithurshen.dev@gmail.com \
    --cc=hsiangkao@linux.alibaba.com \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=xiang@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