From cdb1c79153b2b1377fa0ca7e1beda2b6b9b2c687 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 27 Sep 2019 18:43:07 +0200 Subject: Remove use of ssize_t from library API ssize_t is only available on POSIX platforms and even there it is only defined to hold at least -1 in the range of negative numbers. This commit replaces ssize_t return types with sqfs_s32 and the coresponding function arguments with sqfs_u32. Because the range of positiv numbers for a signed 32 bit number is only half that of the unsigned version, additional checks have to be added. Signed-off-by: David Oberhollenzer --- lib/sqfs/comp/gzip.c | 11 +++++++---- lib/sqfs/comp/lz4.c | 14 ++++++++++---- lib/sqfs/comp/lzma.c | 13 ++++++++----- lib/sqfs/comp/lzo.c | 14 ++++++++++---- lib/sqfs/comp/xz.c | 28 +++++++++++++++++----------- lib/sqfs/comp/zstd.c | 14 ++++++++++---- 6 files changed, 62 insertions(+), 32 deletions(-) (limited to 'lib/sqfs/comp') diff --git a/lib/sqfs/comp/gzip.c b/lib/sqfs/comp/gzip.c index 3dd8d10..3886e60 100644 --- a/lib/sqfs/comp/gzip.c +++ b/lib/sqfs/comp/gzip.c @@ -107,7 +107,7 @@ static int flag_to_zlib_strategy(int flag) } static int find_strategy(gzip_compressor_t *gzip, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { int ret, strategy, selected = Z_DEFAULT_STRATEGY; size_t i, length, minlength = 0; @@ -148,13 +148,16 @@ static int find_strategy(gzip_compressor_t *gzip, const sqfs_u8 *in, return selected; } -static ssize_t gzip_do_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 gzip_do_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { gzip_compressor_t *gzip = (gzip_compressor_t *)base; int ret, strategy = 0; size_t written; + if (size >= 0x7FFFFFFF) + return 0; + if (gzip->compress && gzip->opt.strategies != 0) { strategy = find_strategy(gzip, in, size, out, outsize); if (strategy < 0) @@ -193,7 +196,7 @@ static ssize_t gzip_do_block(sqfs_compressor_t *base, const sqfs_u8 *in, if (gzip->compress && written >= size) return 0; - return (ssize_t)written; + return written; } if (ret != Z_OK && ret != Z_BUF_ERROR) diff --git a/lib/sqfs/comp/lz4.c b/lib/sqfs/comp/lz4.c index 35fd629..dcee7d8 100644 --- a/lib/sqfs/comp/lz4.c +++ b/lib/sqfs/comp/lz4.c @@ -59,12 +59,15 @@ static int lz4_read_options(sqfs_compressor_t *base, sqfs_file_t *file) return 0; } -static ssize_t lz4_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 lz4_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { lz4_compressor_t *lz4 = (lz4_compressor_t *)base; int ret; + if (size >= 0x7FFFFFFF) + return 0; + if (lz4->high_compression) { ret = LZ4_compress_HC((void *)in, (void *)out, size, outsize, LZ4HC_CLEVEL_MAX); @@ -79,12 +82,15 @@ static ssize_t lz4_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, return ret; } -static ssize_t lz4_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 lz4_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { int ret; (void)base; + if (outsize >= 0x7FFFFFFF) + return 0; + ret = LZ4_decompress_safe((void *)in, (void *)out, size, outsize); if (ret < 0) diff --git a/lib/sqfs/comp/lzma.c b/lib/sqfs/comp/lzma.c index f4211fb..0238df0 100644 --- a/lib/sqfs/comp/lzma.c +++ b/lib/sqfs/comp/lzma.c @@ -39,15 +39,15 @@ static int lzma_read_options(sqfs_compressor_t *base, sqfs_file_t *file) return SQFS_ERROR_UNSUPPORTED; } -static ssize_t lzma_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 lzma_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { lzma_compressor_t *lzma = (lzma_compressor_t *)base; lzma_stream strm = LZMA_STREAM_INIT; lzma_options_lzma opt; int ret; - if (outsize < LZMA_HEADER_SIZE) + if (outsize < LZMA_HEADER_SIZE || size >= 0x7FFFFFFF) return 0; lzma_lzma_preset(&opt, LZMA_DEFAULT_LEVEL); @@ -83,8 +83,8 @@ static ssize_t lzma_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, return strm.total_out; } -static ssize_t lzma_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 lzma_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { sqfs_u8 lzma_header[LZMA_HEADER_SIZE]; lzma_stream strm = LZMA_STREAM_INIT; @@ -92,6 +92,9 @@ static ssize_t lzma_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, int ret; (void)base; + if (size >= 0x7FFFFFFF) + return 0; + if (size < sizeof(lzma_header)) return SQFS_ERROR_CORRUPTED; diff --git a/lib/sqfs/comp/lzo.c b/lib/sqfs/comp/lzo.c index 9af3321..498c250 100644 --- a/lib/sqfs/comp/lzo.c +++ b/lib/sqfs/comp/lzo.c @@ -112,13 +112,16 @@ static int lzo_read_options(sqfs_compressor_t *base, sqfs_file_t *file) return 0; } -static ssize_t lzo_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 lzo_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { lzo_compressor_t *lzo = (lzo_compressor_t *)base; lzo_uint len = outsize; int ret; + if (size >= 0x7FFFFFFF) + return 0; + if (lzo->algorithm == SQFS_LZO1X_999 && lzo->level != SQFS_LZO_DEFAULT_LEVEL) { ret = lzo1x_999_compress_level(in, size, out, &len, @@ -138,13 +141,16 @@ static ssize_t lzo_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, return 0; } -static ssize_t lzo_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 lzo_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { lzo_compressor_t *lzo = (lzo_compressor_t *)base; lzo_uint len = outsize; int ret; + if (outsize >= 0x7FFFFFFF) + return 0; + ret = lzo1x_decompress_safe(in, size, out, &len, lzo->buffer); if (ret != LZO_E_OK) diff --git a/lib/sqfs/comp/xz.c b/lib/sqfs/comp/xz.c index d905f2d..9e03df8 100644 --- a/lib/sqfs/comp/xz.c +++ b/lib/sqfs/comp/xz.c @@ -74,9 +74,9 @@ static int xz_read_options(sqfs_compressor_t *base, sqfs_file_t *file) return 0; } -static ssize_t compress(xz_compressor_t *xz, lzma_vli filter, - const sqfs_u8 *in, size_t size, - sqfs_u8 *out, size_t outsize) +static sqfs_s32 compress(xz_compressor_t *xz, lzma_vli filter, + const sqfs_u8 *in, sqfs_u32 size, + sqfs_u8 *out, sqfs_u32 outsize) { lzma_filter filters[5]; lzma_options_lzma opt; @@ -135,13 +135,16 @@ static lzma_vli flag_to_vli(int flag) return LZMA_VLI_UNKNOWN; } -static ssize_t xz_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 xz_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { xz_compressor_t *xz = (xz_compressor_t *)base; lzma_vli filter, selected = LZMA_VLI_UNKNOWN; - size_t i, smallest; - ssize_t ret; + sqfs_s32 ret, smallest; + size_t i; + + if (size >= 0x7FFFFFFF) + return 0; ret = compress(xz, LZMA_VLI_UNKNOWN, in, size, out, outsize); if (ret < 0 || xz->flags == 0) @@ -159,7 +162,7 @@ static ssize_t xz_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, if (ret < 0) return ret; - if (ret > 0 && (smallest == 0 || (size_t)ret < smallest)) { + if (ret > 0 && (smallest == 0 || ret < smallest)) { smallest = ret; selected = filter; } @@ -171,8 +174,8 @@ static ssize_t xz_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, return compress(xz, selected, in, size, out, outsize); } -static ssize_t xz_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 xz_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { sqfs_u64 memlimit = 32 * 1024 * 1024; size_t dest_pos = 0; @@ -180,12 +183,15 @@ static ssize_t xz_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, lzma_ret ret; (void)base; + if (outsize >= 0x7FFFFFFF) + return 0; + ret = lzma_stream_buffer_decode(&memlimit, 0, NULL, in, &src_pos, size, out, &dest_pos, outsize); if (ret == LZMA_OK && size == src_pos) - return (ssize_t)dest_pos; + return dest_pos; return SQFS_ERROR_COMPRESSOR; } diff --git a/lib/sqfs/comp/zstd.c b/lib/sqfs/comp/zstd.c index 81e369b..b2e5d8b 100644 --- a/lib/sqfs/comp/zstd.c +++ b/lib/sqfs/comp/zstd.c @@ -51,12 +51,15 @@ static int zstd_read_options(sqfs_compressor_t *base, sqfs_file_t *file) return 0; } -static ssize_t zstd_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 zstd_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { zstd_compressor_t *zstd = (zstd_compressor_t *)base; size_t ret; + if (size >= 0x7FFFFFFF) + return 0; + ret = ZSTD_compressCCtx(zstd->zctx, out, outsize, in, size, zstd->level); @@ -66,12 +69,15 @@ static ssize_t zstd_comp_block(sqfs_compressor_t *base, const sqfs_u8 *in, return ret < size ? ret : 0; } -static ssize_t zstd_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, - size_t size, sqfs_u8 *out, size_t outsize) +static sqfs_s32 zstd_uncomp_block(sqfs_compressor_t *base, const sqfs_u8 *in, + sqfs_u32 size, sqfs_u8 *out, sqfs_u32 outsize) { size_t ret; (void)base; + if (outsize >= 0x7FFFFFFF) + return 0; + ret = ZSTD_decompress(out, outsize, in, size); if (ZSTD_isError(ret)) -- cgit v1.2.3