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 ++++++++++---- lib/sqfs/data_reader.c | 17 ++++++++++------- lib/sqfs/meta_reader.c | 4 ++-- lib/sqfs/meta_writer.c | 4 ++-- 9 files changed, 76 insertions(+), 43 deletions(-) (limited to 'lib/sqfs') 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)) diff --git a/lib/sqfs/data_reader.c b/lib/sqfs/data_reader.c index 0befb9d..12a8feb 100644 --- a/lib/sqfs/data_reader.c +++ b/lib/sqfs/data_reader.c @@ -40,8 +40,8 @@ static int get_block(sqfs_data_reader_t *data, sqfs_u64 off, sqfs_u32 size, size_t unpacked_size, sqfs_block_t **out) { sqfs_block_t *blk = alloc_flex(sizeof(*blk), 1, unpacked_size); - size_t on_disk_size; - ssize_t ret; + sqfs_u32 on_disk_size; + sqfs_s32 ret; int err; if (blk == NULL) @@ -264,14 +264,17 @@ int sqfs_data_reader_get_fragment(sqfs_data_reader_t *data, return 0; } -ssize_t sqfs_data_reader_read(sqfs_data_reader_t *data, - const sqfs_inode_generic_t *inode, - sqfs_u64 offset, void *buffer, size_t size) +sqfs_s32 sqfs_data_reader_read(sqfs_data_reader_t *data, + const sqfs_inode_generic_t *inode, + sqfs_u64 offset, void *buffer, sqfs_u32 size) { - sqfs_u32 frag_idx, frag_off; - size_t i, diff, total = 0; + sqfs_u32 frag_idx, frag_off, diff, total = 0; sqfs_u64 off, filesz; char *ptr; + size_t i; + + if (size >= 0x7FFFFFFF) + size = 0x7FFFFFFE; /* work out file location and size */ sqfs_inode_get_file_size(inode, &filesz); diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c index d3c3565..b2fdfa9 100644 --- a/lib/sqfs/meta_reader.c +++ b/lib/sqfs/meta_reader.c @@ -71,8 +71,8 @@ int sqfs_meta_reader_seek(sqfs_meta_reader_t *m, sqfs_u64 block_start, { bool compressed; sqfs_u16 header; - ssize_t ret; - size_t size; + sqfs_u32 size; + sqfs_s32 ret; int err; if (block_start < m->start || block_start >= m->limit) diff --git a/lib/sqfs/meta_writer.c b/lib/sqfs/meta_writer.c index a5ce366..3cf3e3e 100644 --- a/lib/sqfs/meta_writer.c +++ b/lib/sqfs/meta_writer.c @@ -89,8 +89,8 @@ void sqfs_meta_writer_destroy(sqfs_meta_writer_t *m) int sqfs_meta_writer_flush(sqfs_meta_writer_t *m) { meta_block_t *outblk; - size_t count; - ssize_t ret; + sqfs_u32 count; + sqfs_s32 ret; if (m->offset == 0) return 0; -- cgit v1.2.3