Commit dc424a6c for xz
commit dc424a6cf9a5eaafb6903d5c3c9b003dcb1f1d75
Author: Lasse Collin <lasse.collin@tukaani.org>
Date: Wed Nov 25 18:36:32 2026 +0200
liblzma: Fix lzma_memlimit_get() after reinit of lzma_auto_decoder()
If lzma_memlimit_get() was called right after reinitialization with
lzma_auto_decoder(), lzma_memlimit_get() returned the old memlimit
instead of the current one.
In debug builds, this resulted in an assertion failure if the new and
old limits weren't equal. The failure occurred with lzma_memusage(),
lzma_memlimit_set(), and lzma_memlimit_get() because they all call
the same internal function.
Reported-by: Pu Cheng
Fixes: https://github.com/tukaani-project/xz/pull/266
diff --git a/src/liblzma/common/auto_decoder.c b/src/liblzma/common/auto_decoder.c
index da49345f..85e6ab73 100644
--- a/src/liblzma/common/auto_decoder.c
+++ b/src/liblzma/common/auto_decoder.c
@@ -140,20 +140,33 @@ auto_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
lzma_ret ret;
if (coder->next.memconfig != NULL) {
+ // In SEQ_INIT, if coder->next contains a coder, it is
+ // the cached coder from previous initialization.
+ //
+ // - It may have used a different memlimit that what is
+ // currently active.
+ //
+ // - We can set the new limit even in SEQ_INIT because
+ // we don't want to accept a limit that is below the
+ // memusage of the cached coder.
+ uint64_t next_old_memlimit;
ret = coder->next.memconfig(coder->next.coder,
- memusage, old_memlimit, new_memlimit);
- assert(*old_memlimit == coder->memlimit);
+ memusage, &next_old_memlimit, new_memlimit);
+
+ assert(coder->sequence == SEQ_INIT
+ || next_old_memlimit == coder->memlimit);
} else {
// No coder is configured yet. Use the base value as
// the current memory usage.
*memusage = LZMA_MEMUSAGE_BASE;
- *old_memlimit = coder->memlimit;
ret = LZMA_OK;
if (new_memlimit != 0 && new_memlimit < *memusage)
ret = LZMA_MEMLIMIT_ERROR;
}
+ *old_memlimit = coder->memlimit;
+
if (ret == LZMA_OK && new_memlimit != 0)
coder->memlimit = new_memlimit;