From: Gao Xiang <hsiangkao@linux.alibaba.com>
To: linux-erofs@lists.ozlabs.org
Cc: Gao Xiang <hsiangkao@linux.alibaba.com>,
Yifan Zhao <zhaoyifan@sjtu.edu.cn>
Subject: [PATCH] erofs-utils: lib: reset hc to avoid 32-bit overflow of kite-deflate
Date: Wed, 24 Jan 2024 17:16:21 +0800 [thread overview]
Message-ID: <20240124091621.2413606-1-hsiangkao@linux.alibaba.com> (raw)
Yifan reported a "segmentation fault (core dumped)" error days ago
with a large dataset (enwik9 x 5). Let's fix it.
Reported-by: Yifan Zhao <zhaoyifan@sjtu.edu.cn>
Fixes: 861037f4fc15 ("erofs-utils: add a built-in DEFLATE compressor")
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
---
include/erofs/defs.h | 3 +++
lib/kite_deflate.c | 12 ++++++++++++
2 files changed, 15 insertions(+)
diff --git a/include/erofs/defs.h b/include/erofs/defs.h
index e7384a1..4ea9a55 100644
--- a/include/erofs/defs.h
+++ b/include/erofs/defs.h
@@ -343,6 +343,9 @@ unsigned long __roundup_pow_of_two(unsigned long n)
#define ST_MTIM_NSEC(stbuf) 0
#endif
+#define likely(x) __builtin_expect(!!(x), 1)
+#define unlikely(x) __builtin_expect(!!(x), 0)
+
#ifdef __cplusplus
}
#endif
diff --git a/lib/kite_deflate.c b/lib/kite_deflate.c
index 8667954..cb81626 100644
--- a/lib/kite_deflate.c
+++ b/lib/kite_deflate.c
@@ -859,6 +859,18 @@ static void kite_mf_reset(struct kite_matchfinder *mf,
*/
mf->base += mf->offset + kHistorySize32 + 1;
+ /*
+ * Unlike other LZ encoders like liblzma [1], we simply reset the hash
+ * chain instead of normalization. This avoids extra complexity, as we
+ * don't consider extreme large input buffers in one go.
+ *
+ * [1] https://github.com/tukaani-project/xz/blob/v5.4.0/src/liblzma/lz/lz_encoder_mf.c#L94
+ */
+ if (unlikely(mf->base > ((typeof(mf->base))-1) >> 1)) {
+ mf->base = kHistorySize32 + 1;
+ memset(mf->hash, 0, 0x10000 * sizeof(mf->hash[0]));
+ memset(mf->chain, 0, sizeof(mf->chain[0]) * mf->wsiz);
+ }
mf->offset = 0;
mf->cyclic_pos = 0;
--
2.39.3
next reply other threads:[~2024-01-24 9:16 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-24 9:16 Gao Xiang [this message]
2024-01-24 9:47 ` [PATCH] erofs-utils: lib: reset hc to avoid 32-bit overflow of kite-deflate Yifan Zhao
2024-01-24 9:53 ` Gao Xiang
2024-01-24 10:40 ` Yifan Zhao
2024-01-24 10:45 ` 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=20240124091621.2413606-1-hsiangkao@linux.alibaba.com \
--to=hsiangkao@linux.alibaba.com \
--cc=linux-erofs@lists.ozlabs.org \
--cc=zhaoyifan@sjtu.edu.cn \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).