- 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 <hsiang...@linux.alibaba.com>
---
 lib/compressor_libdeflate.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/lib/compressor_libdeflate.c b/lib/compressor_libdeflate.c
index f6a7de9d..186da87c 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;
 };
 
@@ -38,7 +40,15 @@ static int libdeflate_compress_destsize(const struct 
erofs_compress *c,
        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;
@@ -51,11 +61,12 @@ static int libdeflate_compress_destsize(const struct 
erofs_compress *c,
                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 +
@@ -102,6 +113,7 @@ static int compressor_libdeflate_exit(struct erofs_compress 
*c)
        if (!ctx)
                return -EINVAL;
        libdeflate_free_compressor(ctx->strm);
+       free(ctx->fitblk_buffer);
        free(ctx);
        return 0;
 }
-- 
2.43.5


Reply via email to