liblzma: Refactor to use lzma_filters_free().
lzma_filters_free() sets the options to NULL and ids to LZMA_VLI_UNKNOWN so there is no need to do it by caller; the filter arrays will always be left in a safe state. Also use memcpy() instead of a loop to copy a filter chain when it is known to be safe to copy LZMA_FILTERS_MAX + 1 (even if the elements past the terminator might be uninitialized).
This commit is contained in:
parent
cb05dbcf8b
commit
e1acf71072
|
@ -219,8 +219,7 @@ stream_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
|
||||||
lzma_next_end(&coder->index_encoder, allocator);
|
lzma_next_end(&coder->index_encoder, allocator);
|
||||||
lzma_index_end(coder->index, allocator);
|
lzma_index_end(coder->index, allocator);
|
||||||
|
|
||||||
for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
|
lzma_filters_free(coder->filters, allocator);
|
||||||
lzma_free(coder->filters[i].options, allocator);
|
|
||||||
|
|
||||||
lzma_free(coder, allocator);
|
lzma_free(coder, allocator);
|
||||||
return;
|
return;
|
||||||
|
@ -271,22 +270,15 @@ stream_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free the options of the old chain.
|
// Free the options of the old chain.
|
||||||
for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
|
lzma_filters_free(coder->filters, allocator);
|
||||||
lzma_free(coder->filters[i].options, allocator);
|
|
||||||
|
|
||||||
// Copy the new filter chain in place.
|
// Copy the new filter chain in place.
|
||||||
size_t j = 0;
|
memcpy(coder->filters, temp, sizeof(temp));
|
||||||
do {
|
|
||||||
coder->filters[j].id = temp[j].id;
|
|
||||||
coder->filters[j].options = temp[j].options;
|
|
||||||
} while (temp[j++].id != LZMA_VLI_UNKNOWN);
|
|
||||||
|
|
||||||
return LZMA_OK;
|
return LZMA_OK;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
for (size_t i = 0; temp[i].id != LZMA_VLI_UNKNOWN; ++i)
|
lzma_filters_free(temp, allocator);
|
||||||
lzma_free(temp[i].options, allocator);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -866,8 +866,7 @@ stream_encoder_mt_end(void *coder_ptr, const lzma_allocator *allocator)
|
||||||
threads_end(coder, allocator);
|
threads_end(coder, allocator);
|
||||||
lzma_outq_end(&coder->outq, allocator);
|
lzma_outq_end(&coder->outq, allocator);
|
||||||
|
|
||||||
for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
|
lzma_filters_free(coder->filters, allocator);
|
||||||
lzma_free(coder->filters[i].options, allocator);
|
|
||||||
|
|
||||||
lzma_next_end(&coder->index_encoder, allocator);
|
lzma_next_end(&coder->index_encoder, allocator);
|
||||||
lzma_index_end(coder->index, allocator);
|
lzma_index_end(coder->index, allocator);
|
||||||
|
@ -1068,13 +1067,7 @@ stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||||
coder->timeout = options->timeout;
|
coder->timeout = options->timeout;
|
||||||
|
|
||||||
// Free the old filter chain and copy the new one.
|
// Free the old filter chain and copy the new one.
|
||||||
for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
|
lzma_filters_free(coder->filters, allocator);
|
||||||
lzma_free(coder->filters[i].options, allocator);
|
|
||||||
|
|
||||||
// Mark it as empty so that it is in a safe state in case
|
|
||||||
// lzma_filters_copy() fails.
|
|
||||||
coder->filters[0].id = LZMA_VLI_UNKNOWN;
|
|
||||||
|
|
||||||
return_if_error(lzma_filters_copy(
|
return_if_error(lzma_filters_copy(
|
||||||
filters, coder->filters, allocator));
|
filters, coder->filters, allocator));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue