diff options
author | Jia Tan <jiat0218@gmail.com> | 2023-06-28 20:43:29 +0800 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2023-07-18 13:26:48 +0300 |
commit | b9499c94fccc03eba5311b5305f04149a7c5af65 (patch) | |
tree | 0b0d0677777f9eb5d1f9b371aced1922b970d259 /tests | |
parent | liblzma: Prevent uninitialzed warning in mt stream encoder. (diff) | |
download | xz-b9499c94fccc03eba5311b5305f04149a7c5af65.tar.xz |
Tests: Fix memory leaks in test_block_header.
test_block_header was not properly freeing the filter options between
calls to lzma_block_header_decode(). The memory leaks were discovered by
using -fsanitize=address with GCC.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_block_header.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/tests/test_block_header.c b/tests/test_block_header.c index 43c7df43..b3101355 100644 --- a/tests/test_block_header.c +++ b/tests/test_block_header.c @@ -17,6 +17,20 @@ static lzma_options_lzma opt_lzma; +// Used in test_lzma_block_header_decode() between tests to ensure +// no artifacts are leftover in the block struct that could influence +// later tests. +#define RESET_BLOCK(block, buf) \ +do { \ + lzma_filter *filters_ = (block).filters; \ + lzma_filters_free(filters_, NULL); \ + memzero((buf), sizeof((buf))); \ + memzero(&(block), sizeof(lzma_block)); \ + (block).filters = filters_; \ + (block).check = LZMA_CHECK_CRC32; \ +} while (0); + + #ifdef HAVE_ENCODERS static lzma_filter filters_none[1] = { { @@ -411,10 +425,7 @@ test_lzma_block_header_decode(void) compare_blocks(&block, &decoded_block); // Reset output buffer and decoded_block - memzero(out, LZMA_BLOCK_HEADER_SIZE_MAX); - memzero(&decoded_block, sizeof(lzma_block)); - decoded_block.filters = decoded_filters; - decoded_block.check = LZMA_CHECK_CRC32; + RESET_BLOCK(decoded_block, out); // Test with compressed size set block.compressed_size = 4096; @@ -425,10 +436,7 @@ test_lzma_block_header_decode(void) LZMA_OK); compare_blocks(&block, &decoded_block); - memzero(out, LZMA_BLOCK_HEADER_SIZE_MAX); - memzero(&decoded_block, sizeof(lzma_block)); - decoded_block.filters = decoded_filters; - decoded_block.check = LZMA_CHECK_CRC32; + RESET_BLOCK(decoded_block, out); // Test with uncompressed size set block.uncompressed_size = 4096; @@ -439,10 +447,7 @@ test_lzma_block_header_decode(void) LZMA_OK); compare_blocks(&block, &decoded_block); - memzero(out, LZMA_BLOCK_HEADER_SIZE_MAX); - memzero(&decoded_block, sizeof(lzma_block)); - decoded_block.filters = decoded_filters; - decoded_block.check = LZMA_CHECK_CRC32; + RESET_BLOCK(decoded_block, out); // Test with multiple filters block.filters = filters_four; @@ -453,10 +458,7 @@ test_lzma_block_header_decode(void) LZMA_OK); compare_blocks(&block, &decoded_block); - memzero(&decoded_block, sizeof(lzma_block)); - decoded_block.filters = decoded_filters; - decoded_block.check = LZMA_CHECK_CRC32; - decoded_block.header_size = lzma_block_header_size_decode(out[0]); + lzma_filters_free(decoded_filters, NULL); // Test with too high version. The decoder will set it to a version // that it supports. @@ -465,6 +467,10 @@ test_lzma_block_header_decode(void) LZMA_OK); assert_uint_eq(decoded_block.version, 1); + // Free the filters for the last time since all other cases should + // result in an error. + lzma_filters_free(decoded_filters, NULL); + // Test bad check type decoded_block.check = INVALID_LZMA_CHECK_ID; assert_lzma_ret(lzma_block_header_decode(&decoded_block, NULL, out), |