From b9499c94fccc03eba5311b5305f04149a7c5af65 Mon Sep 17 00:00:00 2001 From: Jia Tan Date: Wed, 28 Jun 2023 20:43:29 +0800 Subject: 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. --- tests/test_block_header.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) (limited to 'tests') 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), -- cgit v1.2.3