From 2b975a449c17268f943403176a7609079b7af084 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 4 May 2019 15:32:52 +0200 Subject: Remove compressor internal buffers Pass in an external destination buffer + size and allow for propper bounds checking (especially when unpacking). Signed-off-by: David Oberhollenzer --- unpack/extract_file.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'unpack') diff --git a/unpack/extract_file.c b/unpack/extract_file.c index c18ace8..368f6d7 100644 --- a/unpack/extract_file.c +++ b/unpack/extract_file.c @@ -4,18 +4,19 @@ int extract_file(file_info_t *fi, compressor_t *cmp, size_t block_size, frag_reader_t *frag, int sqfsfd, int outfd) { + void *buffer, *scratch, *ptr; size_t i, count, fragsz; bool compressed; - void *buffer; uint32_t bs; ssize_t ret; - buffer = malloc(block_size); + buffer = malloc(block_size * 2); if (buffer == NULL) { perror("allocating scratch buffer"); return -1; } + scratch = (char *)buffer + block_size; count = fi->size / block_size; if (count > 0) { @@ -39,14 +40,18 @@ int extract_file(file_info_t *fi, compressor_t *cmp, size_t block_size, goto fail_trunc; if (compressed) { - ret = cmp->do_block(cmp, buffer, bs); + ret = cmp->do_block(cmp, buffer, bs, + scratch, block_size); if (ret <= 0) goto fail; bs = ret; + ptr = scratch; + } else { + ptr = buffer; } - ret = write_retry(outfd, buffer, bs); + ret = write_retry(outfd, ptr, bs); if (ret < 0) goto fail_wr; -- cgit v1.2.3