From 072d29250113268536719ad0e040ab8a66fb6435 Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Sat, 13 May 2023 19:36:09 +0800 Subject: [PATCH] xz: Use lzma_filters_free() in forget_filter_chain(). This is a little cleaner than the previous implementation of forget_filter_chain(). It is also more consistent since lzma_str_to_filters() will always terminate the filter chain so there is no need to terminate it later in coder_set_compression_settings(). --- src/xz/coder.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/xz/coder.c b/src/xz/coder.c index 159b7d8b..df8a9778 100644 --- a/src/xz/coder.c +++ b/src/xz/coder.c @@ -84,10 +84,9 @@ forget_filter_chain(void) { // Setting a preset or using --filters makes us forget // the earlier custom filter chain (if any). - while (filters_count > 0) { - --filters_count; - free(filters[filters_count].options); - filters[filters_count].options = NULL; + if (filters_count > 0) { + lzma_filters_free(filters, NULL); + filters_count = 0; } string_to_filter_used = false; @@ -125,7 +124,9 @@ coder_add_filter(lzma_vli id, void *options) filters[filters_count].id = id; filters[filters_count].options = options; - ++filters_count; + // Terminate the filter chain with LZMA_VLI_UNKNOWN to simplify + // implementation of forget_filter_chain(). + filters[++filters_count].id = LZMA_VLI_UNKNOWN; // Setting a custom filter chain makes us forget the preset options. // This makes a difference if one specifies e.g. "xz -9 --lzma2 -e" @@ -234,11 +235,12 @@ coder_set_compression_settings(void) filters[0].id = opt_format == FORMAT_LZMA ? LZMA_FILTER_LZMA1 : LZMA_FILTER_LZMA2; filters[0].options = &opt_lzma; - filters_count = 1; - } - // Terminate the filter options array. - filters[filters_count].id = LZMA_VLI_UNKNOWN; + filters_count = 1; + + // Terminate the filter options array. + filters[1].id = LZMA_VLI_UNKNOWN; + } // If we are using the .lzma format, allow exactly one filter // which has to be LZMA1.