Commit 52b65f13 for xz
commit 52b65f131648806254a3566a1dda23df61237c7e
Author: Pu Cheng <217626997+GeekQAQ@users.noreply.github.com>
Date: Fri Nov 20 03:17:13 2026 +0800
liblzma: Free memory and reset counters if lzma_easy_encoder() fails
If the preset was unsupported, lzma_easy_encoder() returned
LZMA_OPTIONS_ERROR without calling lzma_stream_encoder(). If strm was
being reused without calling lzma_end() first, the old encoder was
left allocated and strm->total_in and strm->total_out weren't reset.
lzma/base.h and lzma/container.h say that no memory is left allocated
after a failed initialization. Thus an application that follows the docs
leaked the old coder.
Closes: https://github.com/tukaani-project/xz/pull/265
diff --git a/src/liblzma/common/easy_encoder.c b/src/liblzma/common/easy_encoder.c
index 8dfe2961..dcf5e325 100644
--- a/src/liblzma/common/easy_encoder.c
+++ b/src/liblzma/common/easy_encoder.c
@@ -16,8 +16,18 @@ extern LZMA_API(lzma_ret)
lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
{
lzma_options_easy opt_easy;
- if (lzma_easy_preset(&opt_easy, preset))
+ if (lzma_easy_preset(&opt_easy, preset)) {
+ // Other initialization functions free the memory associated
+ // with *strm and reset the counters also when they fail.
+ // Do the same here as the API docs promise.
+ if (strm != NULL) {
+ lzma_end(strm);
+ strm->total_in = 0;
+ strm->total_out = 0;
+ }
+
return LZMA_OPTIONS_ERROR;
+ }
return lzma_stream_encoder(strm, opt_easy.filters, check);
}