erofs-utils: lib: switch to on-heap fitblk_buffer for libdeflate
- Allocating VLAs on the stack (or using alloca()) for large sizes
could exceed the stack limit;
- It's easier to isolate these buffers on the heap for code sanitizers
to detect potential bugs.
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20250703053446.201941-2-hsiangkao@linux.alibaba.com
diff --git a/lib/compressor_libdeflate.c b/lib/compressor_libdeflate.c
index aaf4684..65531de 100644
--- a/lib/compressor_libdeflate.c
+++ b/lib/compressor_libdeflate.c
@@ -9,6 +9,8 @@
struct erofs_libdeflate_context {
struct libdeflate_compressor *strm;
+ u8 *fitblk_buffer;
+ unsigned int fitblk_bufsiz;
size_t last_uncompressed_size;
};
@@ -21,7 +23,15 @@
size_t l_csize = 0;
size_t r = *srcsize + 1; /* smallest input that doesn't fit so far */
size_t m;
- u8 tmpbuf[dstsize + 9];
+
+ if (dstsize + 9 > ctx->fitblk_bufsiz) {
+ u8 *buf = realloc(ctx->fitblk_buffer, dstsize + 9);
+
+ if (!buf)
+ return -ENOMEM;
+ ctx->fitblk_bufsiz = dstsize + 9;
+ ctx->fitblk_buffer = buf;
+ }
if (ctx->last_uncompressed_size)
m = ctx->last_uncompressed_size * 15 / 16;
@@ -34,11 +44,12 @@
m = min(m, r - 1);
csize = libdeflate_deflate_compress(ctx->strm, src, m,
- tmpbuf, dstsize + 9);
+ ctx->fitblk_buffer,
+ dstsize + 9);
/*printf("Tried %zu => %zu\n", m, csize);*/
if (csize > 0 && csize <= dstsize) {
/* Fits */
- memcpy(dst, tmpbuf, csize);
+ memcpy(dst, ctx->fitblk_buffer, csize);
l = m;
l_csize = csize;
if (r <= l + 1 || csize +
@@ -85,6 +96,7 @@
if (!ctx)
return -EINVAL;
libdeflate_free_compressor(ctx->strm);
+ free(ctx->fitblk_buffer);
free(ctx);
return 0;
}