From 6d1fcf62066cf340bcb1527fae4e366af3013ed6 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Tue, 19 Aug 2014 21:35:51 -0500 Subject: [PATCH] wimlib_get_compressor_needed_memory(): return 0 on invalid max_block_size --- include/wimlib.h | 3 ++- src/compress.c | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/include/wimlib.h b/include/wimlib.h index 5fb97658..3762916a 100644 --- a/include/wimlib.h +++ b/include/wimlib.h @@ -4455,7 +4455,8 @@ wimlib_set_default_compression_level(int ctype, unsigned int compression_level); * wimlib_create_compressor() for the specified compression type, maximum block * size, and compression level. @p compression_level may be 0, in which case * the current default compression level for @p ctype is used. Returns 0 if the - * compression type is invalid. + * compression type is invalid, or the @p max_block_size for that compression + * type is invalid. */ extern uint64_t wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype, diff --git a/src/compress.c b/src/compress.c index efbd2085..aed609aa 100644 --- a/src/compress.c +++ b/src/compress.c @@ -90,6 +90,9 @@ wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype, if (!compressor_ctype_valid(ctype)) return 0; + if (max_block_size == 0) + return 0; + ops = compressor_ops[ctype]; if (compression_level == 0) @@ -97,10 +100,16 @@ wimlib_get_compressor_needed_memory(enum wimlib_compression_type ctype, if (compression_level == 0) compression_level = DEFAULT_COMPRESSION_LEVEL; - size = sizeof(struct wimlib_compressor); - if (ops->get_needed_memory) - size += ops->get_needed_memory(max_block_size, compression_level); - return size; + if (ops->get_needed_memory) { + size = ops->get_needed_memory(max_block_size, compression_level); + + /* 0 is never valid and indicates an invalid max_block_size. */ + if (size == 0) + return 0; + } else { + size = 0; + } + return size + sizeof(struct wimlib_compressor); } WIMLIBAPI int -- 2.43.0