Commit c3b290c3 for xz
commit c3b290c3dc536d16884a6c695618eb08fdbc700a
Author: Lasse Collin <lasse.collin@tukaani.org>
Date: Wed Sep 9 14:14:40 2026 +0300
liblzma: Make lzma_simple_coder_init() reinit-safe after alloc failure
lzma_raw_coder_init() always calls lzma_next_end() if an error occurs
in filter initialization, and no other code path can call
lzma_simple_coder_init(), so this change doesn't fix any bug.
This is just to reduce likelyhood of future bugs.
diff --git a/src/liblzma/simple/simple_coder.c b/src/liblzma/simple/simple_coder.c
index 5cbfa822..48ae90df 100644
--- a/src/liblzma/simple/simple_coder.c
+++ b/src/liblzma/simple/simple_coder.c
@@ -252,6 +252,17 @@ lzma_simple_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
if (coder == NULL)
return LZMA_MEM_ERROR;
+ // Allocate memory for the filter-specific data structure.
+ if (simple_size > 0) {
+ coder->simple = lzma_alloc(simple_size, allocator);
+ if (coder->simple == NULL) {
+ lzma_free(coder, allocator);
+ return LZMA_MEM_ERROR;
+ }
+ } else {
+ coder->simple = NULL;
+ }
+
next->coder = coder;
next->code = &simple_code;
next->end = &simple_coder_end;
@@ -260,15 +271,6 @@ lzma_simple_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
coder->next = LZMA_NEXT_CODER_INIT;
coder->filter = filter;
coder->allocated = 2 * unfiltered_max;
-
- // Allocate memory for filter-specific data structure.
- if (simple_size > 0) {
- coder->simple = lzma_alloc(simple_size, allocator);
- if (coder->simple == NULL)
- return LZMA_MEM_ERROR;
- } else {
- coder->simple = NULL;
- }
}
if (filters[0].options != NULL) {