Commit aeaeb79d for xz

commit aeaeb79deda7262c82afc88640af992be05414ef
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Sat Oct 17 18:37:39 2026 +0300

    liblzma: mt enc: Don't reuse threads on reinit if block size differs

    When a worker thread is created, a per-thread input buffer is allocated.
    Its size is equal to the block size in all threads.

    On reinitialization, the code didn't check if the new block size is
    different than the old block size, and reused the threads even if
    it wasn't. If the new block size was larger than the old one,
    a buffer overflow would occur.

    Typically (including in xz), if the encoder state is reused, it is
    reinitialized with the same options, and then the bug won't be triggered.

    Reported-by: Mar <mardgez13@gmail.com>
    Fixes: de678e0c924a ("liblzma: Add lzma_stream_encoder_mt() for threaded compression.")

diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c
index f520afa7..2d2508cc 100644
--- a/src/liblzma/common/stream_encoder_mt.c
+++ b/src/liblzma/common/stream_encoder_mt.c
@@ -1087,6 +1087,7 @@ stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
 		next->get_progress = &get_progress;
 		next->update = &stream_encoder_mt_update;

+		coder->block_size = 0;
 		coder->filters[0].id = LZMA_VLI_UNKNOWN;
 		coder->filters_cache[0].id = LZMA_VLI_UNKNOWN;
 		coder->index_encoder = LZMA_NEXT_CODER_INIT;
@@ -1097,6 +1098,9 @@ stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
 		coder->threads_initialized = 0;
 	}

+	// Remember the old block size.
+	const size_t old_block_size = coder->block_size;
+
 	// Basic initializations
 	coder->sequence = SEQ_STREAM_HEADER;
 	coder->block_size = (size_t)(block_size);
@@ -1105,8 +1109,15 @@ stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
 	coder->thr = NULL;

 	// Allocate the thread-specific base structures.
+	// If the block size or the number of threads has changed,
+	// don't reuse the old threads:
+	//   - The size of worker_thread.in depends on coder->block_size.
+	//     See initialize_new_thread().
+	//   - coder->threads is allocated below. Its size depends on
+	//     options->threads.
 	assert(options->threads > 0);
-	if (coder->threads_max != options->threads) {
+	if (old_block_size != coder->block_size
+			|| coder->threads_max != options->threads) {
 		threads_end(coder, allocator);

 		coder->threads = NULL;