Commit 29584cda for xz
commit 29584cdaf360e0528859cc962b380a93a6398590
Author: Lasse Collin <lasse.collin@tukaani.org>
Date: Tue May 19 14:04:43 2026 +0300
Tests: Add a test for the lzma_stream_buffer_decode() bug
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 15dc16a8..dd1d4056 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -39,6 +39,7 @@ check_PROGRAMS = \
create_compress_files \
test_check \
test_hardware \
+ test_stream_buffer_decode \
test_stream_flags \
test_filter_flags \
test_filter_str \
@@ -53,6 +54,7 @@ check_PROGRAMS = \
TESTS = \
test_check \
test_hardware \
+ test_stream_buffer_decode \
test_stream_flags \
test_filter_flags \
test_filter_str \
diff --git a/tests/test_stream_buffer_decode.c b/tests/test_stream_buffer_decode.c
new file mode 100644
index 00000000..110bfa02
--- /dev/null
+++ b/tests/test_stream_buffer_decode.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: 0BSD
+
+///////////////////////////////////////////////////////////////////////////////
+//
+/// \file test_stream_buffer_decode.c
+/// \brief Tests lzma_stream_buffer_decode()
+//
+// Author: Lasse Collin
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include "tests.h"
+
+
+// Uncompressed size of the test file
+#define UNCOMP_SIZE 13
+
+// Test file data and its compressed size
+static uint8_t *xz_data;
+static size_t xz_data_size;
+
+
+static void
+test_success(void)
+{
+#ifndef HAVE_DECODERS
+ assert_skip("Decoder support is disabled");
+#else
+ uint64_t memlimit = 1 << 20;
+
+ size_t in_pos = 0;
+
+ uint8_t out[UNCOMP_SIZE];
+ size_t out_pos = 0;
+ size_t out_size = sizeof(out);
+
+ assert_lzma_ret(lzma_stream_buffer_decode(
+ &memlimit, LZMA_CONCATENATED, NULL,
+ xz_data, &in_pos, xz_data_size,
+ out, &out_pos, out_size), LZMA_OK);
+ assert_uint_eq(in_pos, xz_data_size);
+ assert_uint_eq(out_pos, UNCOMP_SIZE);
+#endif
+}
+
+
+static void
+test_data_error(void)
+{
+#ifndef HAVE_DECODERS
+ assert_skip("Decoder support is disabled");
+#else
+ uint64_t memlimit = 1 << 20;
+
+ size_t in_pos = 0;
+
+ uint8_t out[UNCOMP_SIZE];
+ size_t out_pos = 0;
+ size_t out_size = sizeof(out);
+
+ // This fails in 5.8.3 and older.
+ assert_lzma_ret(lzma_stream_buffer_decode(
+ &memlimit, LZMA_CONCATENATED, NULL,
+ xz_data, &in_pos, xz_data_size - 1,
+ out, &out_pos, out_size), LZMA_DATA_ERROR);
+ assert_uint_eq(in_pos, 0);
+ assert_uint_eq(out_pos, 0);
+#endif
+}
+
+
+static void
+test_buf_error(void)
+{
+#ifndef HAVE_DECODERS
+ assert_skip("Decoder support is disabled");
+#else
+ uint64_t memlimit = 1 << 20;
+
+ size_t in_pos = 0;
+
+ uint8_t out[UNCOMP_SIZE - 1];
+ size_t out_pos = 0;
+ size_t out_size = sizeof(out);
+
+ assert_lzma_ret(lzma_stream_buffer_decode(
+ &memlimit, LZMA_CONCATENATED, NULL,
+ xz_data, &in_pos, xz_data_size,
+ out, &out_pos, out_size), LZMA_BUF_ERROR);
+ assert_uint_eq(in_pos, 0);
+ assert_uint_eq(out_pos, 0);
+#endif
+}
+
+
+extern int
+main(int argc, char **argv)
+{
+ tuktest_start(argc, argv);
+
+ xz_data = tuktest_file_from_srcdir("files/good-1-check-crc32.xz",
+ &xz_data_size);
+
+ tuktest_run(test_success);
+ tuktest_run(test_data_error);
+ tuktest_run(test_buf_error);
+
+ return tuktest_end();
+}
diff --git a/tests/tests.cmake b/tests/tests.cmake
index 9302a5e4..eff63d86 100644
--- a/tests/tests.cmake
+++ b/tests/tests.cmake
@@ -61,6 +61,7 @@ if(BUILD_TESTING)
test_index_hash
test_lzip_decoder
test_memlimit
+ test_stream_buffer_decode
test_stream_flags
test_vli
)