From c82907153eaa584f9701f5ba1406ac50a0c73b17 Mon Sep 17 00:00:00 2001 From: AlexGuo1998 Date: Thu, 2 Jan 2025 16:42:28 +0800 Subject: [PATCH] fix: Set LZMA decompressor memory limit to 1GiB (#2044) ### Problem description See #2033 (`hex::dec::lzma_decompress` reports an error when decompressing a small buffer). ### Implementation description Set the LZMA decompressor memory limit to 1GiB fixed. Print a warning when exceeded, and abort with returning `false`. ### Screenshots Normal result when decompressing a small buffer ![image](https://github.com/user-attachments/assets/5b9e6b07-464e-41f6-bdc7-f5b1cd351069) Warning message when `memlimit` is exceeded: (Set to 64B here for a demo) > W: lzma_decompress memory usage 1114168 bytes would exceed the limit (64 bytes), aborting ![image](https://github.com/user-attachments/assets/04abf3ef-1d29-4846-bb41-214695c7d09c) ### Additional things Is the warning wording OK? I'm not a native English speaker so please change it if you want to. --- plugins/decompress/source/content/pl_functions.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/decompress/source/content/pl_functions.cpp b/plugins/decompress/source/content/pl_functions.cpp index d6bc01c4f..0e4f0b6de 100644 --- a/plugins/decompress/source/content/pl_functions.cpp +++ b/plugins/decompress/source/content/pl_functions.cpp @@ -155,7 +155,8 @@ namespace hex::plugin::decompress { auto §ion = evaluator->getSection(params[1].toUnsigned()); lzma_stream stream = LZMA_STREAM_INIT; - if (lzma_auto_decoder(&stream, 0x10000, LZMA_IGNORE_CHECK) != Z_OK) { + constexpr int64_t memlimit = 0x40000000; // 1GiB + if (lzma_auto_decoder(&stream, memlimit, LZMA_IGNORE_CHECK) != LZMA_OK) { return false; } @@ -179,8 +180,8 @@ namespace hex::plugin::decompress { if (res == LZMA_MEMLIMIT_ERROR) { auto usage = lzma_memusage(&stream); - lzma_memlimit_set(&stream, usage); - res = lzma_code(&stream, LZMA_RUN); + evaluator->getConsole().log(pl::core::LogConsole::Level::Warning, fmt::format("lzma_decompress memory usage {} bytes would exceed the limit ({} bytes), aborting", usage, memlimit)); + return false; } if (res != LZMA_OK)