Commit c61d83b2 for xz

commit c61d83b20fa2903be438752e827f67232e514f96
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Wed Nov 25 18:36:32 2026 +0200

    liblzma: Free cached memory if new memlimit is low

    If a decoder was reinitialized with a memory limit that was lower than
    the size of the cached allocations, the memory usage of the freshly-
    reinitialized decoder would be above the new limit. Fix by freeing the
    cached memory if its memory usage is higher than the new limit.

    Side note: If lzma_memlimit_set() is called with a memlimit that is
    lower than the size of the cached allocations, the call will fail.
    It isn't worth it to make lzma_memlimit_set() free the cached allocations
    to meet the new memlimit.

diff --git a/src/liblzma/common/alone_decoder.c b/src/liblzma/common/alone_decoder.c
index f7e5c4ec..8d68b620 100644
--- a/src/liblzma/common/alone_decoder.c
+++ b/src/liblzma/common/alone_decoder.c
@@ -228,6 +228,13 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
 		coder->memusage = LZMA_MEMUSAGE_BASE;
 	}

+	// On reinitialization, free the cached memory if its memory usage
+	// exceeds the new memlimit.
+	if (coder->memusage > memlimit) {
+		lzma_next_end(&coder->next, allocator);
+		coder->memusage = LZMA_MEMUSAGE_BASE;
+	}
+
 	coder->sequence = SEQ_PROPERTIES;
 	coder->picky = picky;
 	coder->pos = 0;
diff --git a/src/liblzma/common/auto_decoder.c b/src/liblzma/common/auto_decoder.c
index 85e6ab73..69ab680b 100644
--- a/src/liblzma/common/auto_decoder.c
+++ b/src/liblzma/common/auto_decoder.c
@@ -197,6 +197,22 @@ auto_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
 		coder->next = LZMA_NEXT_CODER_INIT;
 	}

+	// On reinitialization, free the cached memory if its memory usage
+	// exceeds the new memlimit.
+	if (coder->next.memconfig != NULL) {
+		uint64_t memusage;
+		uint64_t old_memlimit; // Will be ignored.
+		const lzma_ret ret = coder->next.memconfig(coder->next.coder,
+				&memusage, &old_memlimit, 0);
+		if (ret != LZMA_OK) {
+			assert(0);
+			return LZMA_PROG_ERROR;
+		}
+
+		if (memusage > memlimit)
+			lzma_next_end(&coder->next, allocator);
+	}
+
 	coder->memlimit = my_max(1, memlimit);
 	coder->flags = flags;
 	coder->sequence = SEQ_INIT;
diff --git a/src/liblzma/common/lzip_decoder.c b/src/liblzma/common/lzip_decoder.c
index ae75df31..1c6049cf 100644
--- a/src/liblzma/common/lzip_decoder.c
+++ b/src/liblzma/common/lzip_decoder.c
@@ -394,6 +394,13 @@ lzma_lzip_decoder_init(
 		coder->memusage = LZMA_MEMUSAGE_BASE;
 	}

+	// On reinitialization, free the cached memory if its memory usage
+	// exceeds the new memlimit.
+	if (coder->memusage > memlimit) {
+		lzma_next_end(&coder->lzma_decoder, allocator);
+		coder->memusage = LZMA_MEMUSAGE_BASE;
+	}
+
 	coder->sequence = SEQ_ID_STRING;
 	coder->memlimit = my_max(1, memlimit);
 	coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
diff --git a/src/liblzma/common/stream_decoder.c b/src/liblzma/common/stream_decoder.c
index 2c5485b0..3b806b08 100644
--- a/src/liblzma/common/stream_decoder.c
+++ b/src/liblzma/common/stream_decoder.c
@@ -444,6 +444,13 @@ lzma_stream_decoder_init(
 		coder->memusage = LZMA_MEMUSAGE_BASE;
 	}

+	// On reinitialization, free the cached memory if its memory usage
+	// exceeds the new memlimit.
+	if (coder->memusage > memlimit) {
+		lzma_next_end(&coder->block_decoder, allocator);
+		coder->memusage = LZMA_MEMUSAGE_BASE;
+	}
+
 	coder->memlimit = my_max(1, memlimit);
 	coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
 	coder->tell_unsupported_check
diff --git a/src/liblzma/common/stream_decoder_mt.c b/src/liblzma/common/stream_decoder_mt.c
index 44696bd9..26d50fae 100644
--- a/src/liblzma/common/stream_decoder_mt.c
+++ b/src/liblzma/common/stream_decoder_mt.c
@@ -1946,11 +1946,16 @@ stream_decoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
 	// file could allocate a lot, the next files could use fewer
 	// threads and some of the allocations from the first file would not
 	// get freed unless memlimit_threading forces us to clear caches).
-	//
-	// NOTE: The direct mode decoder isn't freed here if one exists.
-	// It will be reused or freed as needed in the main loop.
 	threads_end(coder, allocator);

+	// Free the direct mode decoder only if its memory usage exceeds
+	// the new memory usage limit. Otherwise it will be reused or freed
+	// as needed in the main loop.
+	if (coder->mem_direct_mode > options->memlimit_stop) {
+		lzma_next_end(&coder->block_decoder, allocator);
+		coder->mem_direct_mode = 0;
+	}
+
 	// All memusage counters start at 0 (including mem_direct_mode).
 	// The little extra that is needed for the structs in this file
 	// get accounted well enough by the filter chain memory usage