Commit 4083cbd8 for xz

commit 4083cbd8b3ec717e90a26ee614d3affcba016c9c
Author: Lasse Collin <lasse.collin@tukaani.org>
Date:   Mon Nov 23 18:02:48 2026 +0200

    liblzma: Reserve memory for match finder prefetching

    Extra bytes were already reserved for lzma_memcmplen(). Define a new
    constant LZ_EXTRA_SIZE that is large enough for both lzma_memcmplen()
    and for prefetching.

diff --git a/src/liblzma/lz/lz_encoder.c b/src/liblzma/lz/lz_encoder.c
index 3ea9ec21..427a4e2f 100644
--- a/src/liblzma/lz/lz_encoder.c
+++ b/src/liblzma/lz/lz_encoder.c
@@ -21,6 +21,13 @@

 #include "memcmplen.h"

+// lzma_memcmplen() may read and ignore extra bytes.
+// The hash prefetching in match finders can read extra bytes too.
+// We need to allocate extra space to allow these reads.
+// The extra space must also be initialized to keep Valgrind
+// and sanitizers happy.
+#define LZ_EXTRA_SIZE my_max(LZ_PF_OFF, LZMA_MEMCMPLEN_EXTRA)
+

 typedef struct {
 	/// LZ-based encoder e.g. LZMA
@@ -109,11 +116,9 @@ fill_window(lzma_coder *coder, const lzma_allocator *allocator,

 	coder->mf.write_pos = write_pos;

-	// Silence Valgrind. lzma_memcmplen() can read extra bytes
-	// and Valgrind will give warnings if those bytes are uninitialized
-	// because Valgrind cannot see that the values of the uninitialized
-	// bytes are eventually ignored.
-	memzero(coder->mf.buffer + write_pos, LZMA_MEMCMPLEN_EXTRA);
+	// lzma_memcmplen() and match finder prefetching can read extra bytes.
+	// Initialize the memory to keep Valgrind and sanitizers happy.
+	memzero(coder->mf.buffer + write_pos, LZ_EXTRA_SIZE);

 	// If end of stream has been reached or flushing completed, we allow
 	// the encoder to process all the input (that is, read_pos is allowed
@@ -374,11 +379,7 @@ lz_encoder_init(lzma_mf *mf, const lzma_allocator *allocator,
 {
 	// Allocate the history buffer.
 	if (mf->buffer == NULL) {
-		// lzma_memcmplen() is used for the dictionary buffer
-		// so we need to allocate a few extra bytes to prevent
-		// it from reading past the end of the buffer.
-		mf->buffer = lzma_alloc(mf->size + LZMA_MEMCMPLEN_EXTRA,
-				allocator);
+		mf->buffer = lzma_alloc(mf->size + LZ_EXTRA_SIZE, allocator);
 		if (mf->buffer == NULL)
 			return true;
 	}
@@ -448,7 +449,7 @@ lz_encoder_init(lzma_mf *mf, const lzma_allocator *allocator,
 				mf->write_pos);

 		// Keep Valgrind and sanitizers happy.
-		memzero(mf->buffer + mf->write_pos, LZMA_MEMCMPLEN_EXTRA);
+		memzero(mf->buffer + mf->write_pos, LZ_EXTRA_SIZE);

 		mf->action = LZMA_SYNC_FLUSH;
 		mf->skip(mf, mf->write_pos);