Commit d5d2aeee for xz
commit d5d2aeee83fc14182b694a80d467e81cdb614f1d
Author: Lasse Collin <lasse.collin@tukaani.org>
Date: Wed Nov 25 18:36:32 2026 +0200
liblzma: Make lzma_memusage() account cached memory
lzma_stream_decoder(), lzma_alone_decoder(), and lzma_lzip_decoder():
If lzma_memusage() was called immediately after a decoder was
reinitialized, the memory used by the still-allocated old decoder
was ignored and thus the return value was misleading. Once a few
bytes of input had been consumed and the memory usage of the new
input was known, then the cached allocation was either reused or
freed, and lzma_memusage() returned correct information.
lzma_stream_decoder_mt() and lzma_auto_decoder() already counted
the cached memory this way. To fix the inconsistency, the other
three were modified to have this behavior too.
This also affects the behavior of lzma_memlimit_set() if it is called
immediately after decoder reinitialization. With the decoders that
didn't account for the cached memory, lzma_memlimit_set() could be
used to set the memlimit to a much smaller value than the amount
of cached allocations. Now it will fail with LZMA_MEMLIMIT_ERROR.
In practice this shouldn't matter. The main use case is to increase
the limit after lzma_code() has returned LZMA_MEMLIMIT_ERROR.
Decreasing the limit with lzma_memlimit_set() is possible but it's
a bit weird thing to do.
diff --git a/src/liblzma/common/alone_decoder.c b/src/liblzma/common/alone_decoder.c
index 174b3b1d..f7e5c4ec 100644
--- a/src/liblzma/common/alone_decoder.c
+++ b/src/liblzma/common/alone_decoder.c
@@ -225,6 +225,7 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
next->end = &alone_decoder_end;
next->memconfig = &alone_decoder_memconfig;
coder->next = LZMA_NEXT_CODER_INIT;
+ coder->memusage = LZMA_MEMUSAGE_BASE;
}
coder->sequence = SEQ_PROPERTIES;
@@ -235,7 +236,6 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
coder->options.preset_dict_size = 0;
coder->uncompressed_size = 0;
coder->memlimit = my_max(1, memlimit);
- coder->memusage = LZMA_MEMUSAGE_BASE;
return LZMA_OK;
}
diff --git a/src/liblzma/common/lzip_decoder.c b/src/liblzma/common/lzip_decoder.c
index d6f929d2..ae75df31 100644
--- a/src/liblzma/common/lzip_decoder.c
+++ b/src/liblzma/common/lzip_decoder.c
@@ -391,11 +391,11 @@ lzma_lzip_decoder_init(
next->memconfig = &lzip_decoder_memconfig;
coder->lzma_decoder = LZMA_NEXT_CODER_INIT;
+ coder->memusage = LZMA_MEMUSAGE_BASE;
}
coder->sequence = SEQ_ID_STRING;
coder->memlimit = my_max(1, memlimit);
- coder->memusage = LZMA_MEMUSAGE_BASE;
coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
diff --git a/src/liblzma/common/stream_decoder.c b/src/liblzma/common/stream_decoder.c
index 94004b74..2c5485b0 100644
--- a/src/liblzma/common/stream_decoder.c
+++ b/src/liblzma/common/stream_decoder.c
@@ -441,10 +441,10 @@ lzma_stream_decoder_init(
coder->block_decoder = LZMA_NEXT_CODER_INIT;
coder->index_hash = NULL;
+ coder->memusage = LZMA_MEMUSAGE_BASE;
}
coder->memlimit = my_max(1, memlimit);
- coder->memusage = LZMA_MEMUSAGE_BASE;
coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
coder->tell_unsupported_check
= (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;