From c3b290c3dc536d16884a6c695618eb08fdbc700a Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Wed, 9 Sep 2026 14:14:40 +0300 Subject: [PATCH] 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. --- src/liblzma/simple/simple_coder.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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) {