Commit a7053889 for xz

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

    Tests: Test the lzma_memusage() bug in lzma_stream_decoder_mt()

    This fails before the previous commit.

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

diff --git a/tests/test_memlimit.c b/tests/test_memlimit.c
index 7e8f8620..32f20903 100644
--- a/tests/test_memlimit.c
+++ b/tests/test_memlimit.c
@@ -16,6 +16,10 @@
 #define MEMLIMIT_TOO_LOW 1234U
 #define MEMLIMIT_HIGH_ENOUGH (2U << 20)

+// LZMA_MEMUSAGE_BASE is 32 KiB in src/liblzma/common/common.h.
+// This limit is higher than the base but still too low.
+#define MEMLIMIT_ABOVE_BASE 50000
+

 static uint8_t *in;
 static size_t in_size;
@@ -91,6 +95,29 @@ test_memlimit_stream_decoder_mt(void)

 	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
 	lzma_end(&strm);
+
+	// Test that lzma_memusage() and lzma_memlimit_set() account for the
+	// next Block's filter chain memory usage. This fails before the commit
+	// "liblzma: mt dec: Fix lzma_memusage() after LZMA_MEMLIMIT_ERROR".
+	mt.memlimit_stop = MEMLIMIT_ABOVE_BASE;
+	assert_lzma_ret(lzma_stream_decoder_mt(&strm, &mt), LZMA_OK);
+
+	strm.next_in = in;
+	strm.avail_in = in_size;
+	strm.next_out = out;
+	strm.avail_out = sizeof(out);
+
+	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_MEMLIMIT_ERROR);
+
+	const uint64_t memusage = lzma_memusage(&strm);
+	assert_uint(memusage, >, MEMLIMIT_ABOVE_BASE);
+	assert_uint_eq(lzma_memlimit_get(&strm), MEMLIMIT_ABOVE_BASE);
+	assert_lzma_ret(lzma_memlimit_set(&strm, memusage - 1),
+			LZMA_MEMLIMIT_ERROR);
+	assert_lzma_ret(lzma_memlimit_set(&strm, memusage), LZMA_OK);
+
+	assert_lzma_ret(lzma_code(&strm, LZMA_FINISH), LZMA_STREAM_END);
+	lzma_end(&strm);
 #endif
 }