1
0
Fork 0
mirror of https://git.tukaani.org/xz.git synced 2024-04-04 12:36:23 +02:00

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.
This commit is contained in:
Jia Tan 2023-06-28 20:43:29 +08:00 committed by Lasse Collin
parent 1155471651
commit b9499c94fc

View file

@ -17,6 +17,20 @@
static lzma_options_lzma opt_lzma; 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 #ifdef HAVE_ENCODERS
static lzma_filter filters_none[1] = { static lzma_filter filters_none[1] = {
{ {
@ -411,10 +425,7 @@ test_lzma_block_header_decode(void)
compare_blocks(&block, &decoded_block); compare_blocks(&block, &decoded_block);
// Reset output buffer and decoded_block // Reset output buffer and decoded_block
memzero(out, LZMA_BLOCK_HEADER_SIZE_MAX); RESET_BLOCK(decoded_block, out);
memzero(&decoded_block, sizeof(lzma_block));
decoded_block.filters = decoded_filters;
decoded_block.check = LZMA_CHECK_CRC32;
// Test with compressed size set // Test with compressed size set
block.compressed_size = 4096; block.compressed_size = 4096;
@ -425,10 +436,7 @@ test_lzma_block_header_decode(void)
LZMA_OK); LZMA_OK);
compare_blocks(&block, &decoded_block); compare_blocks(&block, &decoded_block);
memzero(out, LZMA_BLOCK_HEADER_SIZE_MAX); RESET_BLOCK(decoded_block, out);
memzero(&decoded_block, sizeof(lzma_block));
decoded_block.filters = decoded_filters;
decoded_block.check = LZMA_CHECK_CRC32;
// Test with uncompressed size set // Test with uncompressed size set
block.uncompressed_size = 4096; block.uncompressed_size = 4096;
@ -439,10 +447,7 @@ test_lzma_block_header_decode(void)
LZMA_OK); LZMA_OK);
compare_blocks(&block, &decoded_block); compare_blocks(&block, &decoded_block);
memzero(out, LZMA_BLOCK_HEADER_SIZE_MAX); RESET_BLOCK(decoded_block, out);
memzero(&decoded_block, sizeof(lzma_block));
decoded_block.filters = decoded_filters;
decoded_block.check = LZMA_CHECK_CRC32;
// Test with multiple filters // Test with multiple filters
block.filters = filters_four; block.filters = filters_four;
@ -453,10 +458,7 @@ test_lzma_block_header_decode(void)
LZMA_OK); LZMA_OK);
compare_blocks(&block, &decoded_block); compare_blocks(&block, &decoded_block);
memzero(&decoded_block, sizeof(lzma_block)); lzma_filters_free(decoded_filters, NULL);
decoded_block.filters = decoded_filters;
decoded_block.check = LZMA_CHECK_CRC32;
decoded_block.header_size = lzma_block_header_size_decode(out[0]);
// Test with too high version. The decoder will set it to a version // Test with too high version. The decoder will set it to a version
// that it supports. // that it supports.
@ -465,6 +467,10 @@ test_lzma_block_header_decode(void)
LZMA_OK); LZMA_OK);
assert_uint_eq(decoded_block.version, 1); 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 // Test bad check type
decoded_block.check = INVALID_LZMA_CHECK_ID; decoded_block.check = INVALID_LZMA_CHECK_ID;
assert_lzma_ret(lzma_block_header_decode(&decoded_block, NULL, out), assert_lzma_ret(lzma_block_header_decode(&decoded_block, NULL, out),