Skip to content

Commit

Permalink
fix: Set LZMA decompressor memory limit to 1GiB fixed
Browse files Browse the repository at this point in the history
As discussed in WerWolv#2033, we should limit the memory usage to a reasonably sane
value (1GiB here), rather than always increasing it when exceeded. We also
print a warning to the console in this case. This also avoided the bug when
decompressing a small buffer.
  • Loading branch information
AlexGuo1998 committed Dec 31, 2024
1 parent 655e068 commit c327340
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions plugins/decompress/source/content/pl_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ namespace hex::plugin::decompress {
auto &section = 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;
}

Expand All @@ -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)
Expand Down

0 comments on commit c327340

Please sign in to comment.