Commit 8d19cef9 for xz

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

    Tests: Add a test for the preset dictionary sanitizer issue

    Before the previous commit, this could fail with -fsanitize=memory or
    with Valgrind. The only harm of the bug was sanitizer and Valgrind noise.

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3490569f..889b767c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -40,6 +40,7 @@ check_PROGRAMS = \
 	create_compress_files \
 	test_check \
 	test_hardware \
+	test_raw \
 	test_stream_buffer_decode \
 	test_stream_buffer_encode \
 	test_stream_flags \
@@ -57,6 +58,7 @@ check_PROGRAMS = \
 TESTS = \
 	test_check \
 	test_hardware \
+	test_raw \
 	test_stream_buffer_decode \
 	test_stream_buffer_encode \
 	test_stream_flags \
diff --git a/tests/test_raw.c b/tests/test_raw.c
new file mode 100644
index 00000000..a2ca73fa
--- /dev/null
+++ b/tests/test_raw.c
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: 0BSD
+
+///////////////////////////////////////////////////////////////////////////////
+//
+/// \file       test_raw.c
+/// \brief      Test raw encoding and decoding
+//
+//  Author:     Lasse Collin
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include "tests.h"
+
+
+static const uint8_t preset_dict[] = { 1, 2, 3, 4, 1, 2, 3, 4 };
+
+static uint8_t in[16] = { 1, 2, 3, 4,  1, 2, 3, 4,
+			  9, 0, 0, 0,  0, 0, 0, 0 };
+static uint8_t encoded[128];
+static uint8_t decoded[16];
+
+
+// Before the commit
+// "liblzma: Silence Valgrind and -fsanitize=memory with a preset dictionary":
+// This can fail with -fsanitize=memory or with Valgrind due to the use of
+// an uninitialized value. The uninitialized value was eventually ignored,
+// so the sanitizer and Valgrind errors were the only real problem.
+static void
+test_preset_dict(void)
+{
+	lzma_options_lzma opt_lzma2;
+	assert_false(lzma_lzma_preset(&opt_lzma2, 4));
+
+	opt_lzma2.dict_size = 4096;
+	opt_lzma2.nice_len = 4;
+
+	opt_lzma2.preset_dict = preset_dict;
+	opt_lzma2.preset_dict_size = sizeof(preset_dict);
+
+	lzma_filter filters[] = {
+		{ .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
+		{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
+	};
+
+	size_t enc_size = 0;
+	assert_lzma_ret(lzma_raw_buffer_encode(filters, NULL, in, sizeof(in),
+			encoded, &enc_size, sizeof(encoded)), LZMA_OK);
+	assert_uint(enc_size, >, 0);
+	assert_uint(enc_size, <=, sizeof(encoded));
+
+	size_t in_pos = 0;
+	size_t dec_size = 0;
+	assert_lzma_ret(lzma_raw_buffer_decode(filters, NULL,
+			encoded, &in_pos, enc_size,
+			decoded, &dec_size, sizeof(decoded)), LZMA_OK);
+	assert_uint_eq(in_pos, enc_size);
+	assert_uint_eq(dec_size, sizeof(in));
+	assert_array_eq(in, decoded, sizeof(in));
+
+	// Decoding without a preset dictionary will fail because the raw
+	// LZMA2 stream doesn't start with a dictionary reset.
+	opt_lzma2.preset_dict = NULL;
+	opt_lzma2.preset_dict_size = 0;
+	in_pos = 0;
+	dec_size = 0;
+	assert_lzma_ret(lzma_raw_buffer_decode(filters, NULL,
+			encoded, &in_pos, enc_size,
+			decoded, &dec_size, sizeof(decoded)), LZMA_DATA_ERROR);
+}
+
+
+extern int
+main(int argc, char **argv)
+{
+	tuktest_start(argc, argv);
+	tuktest_run(test_preset_dict);
+	return tuktest_end();
+}
diff --git a/tests/tests.cmake b/tests/tests.cmake
index 061b1ebe..868f9fbb 100644
--- a/tests/tests.cmake
+++ b/tests/tests.cmake
@@ -62,6 +62,7 @@ if(BUILD_TESTING)
         test_index_hash
         test_lzip_decoder
         test_memlimit
+        test_raw
         test_stream_buffer_decode
         test_stream_buffer_encode
         test_stream_flags