Commit 5b4fff9e for xz

commit 5b4fff9eb11a57fae5363a40cb77972d04178250
Author: hyc-alt <250169095+hyc-alt@users.noreply.github.com>
Date:   Fri Nov 27 18:48:45 2026 +0800

    liblzma: mt dec: Fix lzma_memusage() after LZMA_MEMLIMIT_ERROR

    If lzma_code() returned LZMA_MEMLIMIT_ERROR, a call to lzma_memusage()
    didn't return the required amount of memory (it could return a small
    value like LZMA_MEMLIMIT_BASE i.e. 32 KiB). Using lzma_memlimit_set()
    with the bogus value succeeded, but attempting to continue decoding
    kept failing with LZMA_MEMLIMIT_ERROR.

    In xz, the nonsense from lzma_memusage() was seen in an error message:

        $ echo foo | xz > foo.xz
        $ xz --test --memlimit=5MiB foo.xz
        xz: foo.xz: Memory usage limit reached
        xz: 1 MiB of memory is required. The limit is 5 MiB.

    LZMA_MEMLIMIT_ERROR may be returned at the beginning of SEQ_BLOCK_INIT.
    There the amount of memory required for the filter chain (and thus the
    single-threaded direct mode) is available in coder->mem_next_filters.
    Treat SEQ_BLOCK_INIT specially in _memconfig() and ensure that the
    reported memusage is at least mem_next_filters.

    Co-authored-by: Lasse Collin <lasse.collin@tukaani.org>
    Link: https://github.com/tukaani-project/xz/pull/268

diff --git a/src/liblzma/common/stream_decoder_mt.c b/src/liblzma/common/stream_decoder_mt.c
index 7f0f28bb..43502fee 100644
--- a/src/liblzma/common/stream_decoder_mt.c
+++ b/src/liblzma/common/stream_decoder_mt.c
@@ -1841,6 +1841,14 @@ stream_decoder_mt_memconfig(void *coder_ptr, uint64_t *memusage,
 				+ coder->outq.mem_allocated;
 	}

+	// If decoding stopped in SEQ_BLOCK_INIT because mem_next_filters
+	// exceeded memlimit_stop, report at least mem_next_filters so that
+	// lzma_memusage() returns the required memory and lzma_memlimit_set()
+	// rejects limits that are still too low.
+	if (coder->sequence == SEQ_BLOCK_INIT
+			&& *memusage < coder->mem_next_filters)
+		*memusage = coder->mem_next_filters;
+
 	// If no filter chains are allocated, *memusage may be zero.
 	// Always return at least LZMA_MEMUSAGE_BASE.
 	if (*memusage < LZMA_MEMUSAGE_BASE)