From: Utkal Singh <singhutkal015@gmail.com>
To: linux-erofs@lists.ozlabs.org
Cc: xiang@kernel.org, Utkal Singh <singhutkal015@gmail.com>
Subject: [PATCH] erofs-utils: lib: fix undefined behavior in kite_deflate writebits()
Date: Wed, 4 Mar 2026 17:03:27 +0000 [thread overview]
Message-ID: <20260304170327.14908-1-singhutkal015@gmail.com> (raw)
When bitpos equals sizeof(inflightbits) * 8 (i.e., 32), the shift
'v << s->bitpos' has undefined behavior per C11 6.5.7p3, which states
that if the shift amount is greater than or equal to the width of the
promoted left operand, the behavior is undefined.
The original code used a branchless mask '(!rem - 1)' to zero out the
result when rem == 0, but the undefined shift is still evaluated before
the mask is applied. Replace with a conditional to avoid the shift
entirely when rem is zero.
Found via UBSan (-fsanitize=undefined). Similar to commit 2cd5114
("lib: fix undefined behavior in zstd dict_size bit shift").
Signed-off-by: Utkal Singh <singhutkal015@gmail.com>
---
lib/kite_deflate.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/kite_deflate.c b/lib/kite_deflate.c
index f9eb3fb..29e44b3 100644
--- a/lib/kite_deflate.c
+++ b/lib/kite_deflate.c
@@ -144,7 +144,7 @@ static void writebits(struct kite_deflate *s, unsigned int v, u8 bits)
{
unsigned int rem = sizeof(s->inflightbits) * 8 - s->bitpos;
- s->inflightbits |= (v << s->bitpos) & (!rem - 1);
+ s->inflightbits |= rem ? (v << s->bitpos) : 0;
if (bits > rem) {
u8 *out = s->out + s->pos_out;
--
2.43.0
reply other threads:[~2026-03-04 17:03 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20260304170327.14908-1-singhutkal015@gmail.com \
--to=singhutkal015@gmail.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