From 1a7c640ef92068ab0fb9b3aa479eedd38b120c0f Mon Sep 17 00:00:00 2001 From: Roy Oursler Date: Mon, 5 Jun 2017 13:05:20 -0700 Subject: [PATCH] igzip: Fix 0 length file and looping errors in igzip_inflate_test Change-Id: I328f241ba07d8a0ae4fbc4c7de2ea8913912a188 Signed-off-by: Roy Oursler --- igzip/igzip_inflate_test.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/igzip/igzip_inflate_test.c b/igzip/igzip_inflate_test.c index 9aace90..b6e73f2 100644 --- a/igzip/igzip_inflate_test.c +++ b/igzip/igzip_inflate_test.c @@ -229,6 +229,12 @@ int main(int argc, char **argv) if (argc == 1) printf("Error, no input file\n"); for (i = 1; i < argc; i++) { + + file = NULL; + uncompressed_stream = NULL; + compressed_stream = NULL; + uncompressed_test_stream = NULL; + file = fopen(argv[i], "r"); if (file == NULL) { printf("Error opening file %s\n", argv[i]); @@ -246,23 +252,25 @@ int main(int argc, char **argv) } compressed_length = compressBound(file_length); + if (file_length != 0) { uncompressed_stream = malloc(file_length); uncompressed_test_stream = malloc(file_length); } + compressed_stream = malloc(compressed_length); if (uncompressed_stream == NULL && file_length != 0) { - printf("\nFailed to allocate memory\n"); + printf("\nFailed to allocate input memory\n"); exit(0); } if (compressed_stream == NULL) { - printf("\nFailed to allocate memory\n"); + printf("\nFailed to allocate output memory\n"); exit(0); } - if (uncompressed_test_stream == NULL) { - printf("\nFailed to allocate memory\n"); + if (uncompressed_test_stream == NULL && file_length != 0) { + printf("\nFailed to allocate decompressed memory\n"); exit(0); } @@ -285,7 +293,8 @@ int main(int argc, char **argv) fflush(0); fclose(file); - free(compressed_stream); + if (compressed_stream != NULL) + free(compressed_stream); if (uncompressed_stream != NULL) free(uncompressed_stream); if (uncompressed_test_stream != NULL)