diff --git a/igzip/igzip_inflate_test.c b/igzip/igzip_inflate_test.c index 0bf7817..5c334a6 100644 --- a/igzip/igzip_inflate_test.c +++ b/igzip/igzip_inflate_test.c @@ -240,6 +240,16 @@ main(int argc, char **argv) printf("Starting file %s", argv[i]); fflush(0); file_length = get_filesize(file); + if (file_length == 0) { + /* Check if it's a real error or just an empty file */ + if (fseek(file, 0, SEEK_END) != 0 || ftell(file) < 0) + printf("\nFailed to get file size.\n"); + else + printf("\nFile has zero length.\n"); + printf(" ... Fail\n"); + fclose(file); + continue; + } if (file_length > MAX_INPUT_FILE_SIZE) { printf("\nFile too large to run on this test," " Max 512MB for 32bit OS, 2GB for 64bit OS.\n"); diff --git a/igzip/igzip_perf.c b/igzip/igzip_perf.c index 429881f..3440e20 100644 --- a/igzip/igzip_perf.c +++ b/igzip/igzip_perf.c @@ -725,8 +725,16 @@ main(int argc, char *argv[]) exit(1); } dict_file_size = get_filesize(dict_fn); + if (dict_file_size == 0) { + /* Check if it's a real error or just an empty file */ + if (fseek(dict_fn, 0, SEEK_END) != 0 || ftell(dict_fn) < 0) + printf("Failed to get dictionary file size\n"); + else + printf("Dictionary file has zero length\n"); + exit(1); + } dict_buf = malloc(dict_file_size); - if (dict_buf == NULL || dict_file_size == 0) { + if (dict_buf == NULL) { printf("Can't allocate mem for dictionary buffer\n"); exit(1); } @@ -795,7 +803,11 @@ main(int argc, char *argv[]) info.file_size = get_filesize(in); if (info.file_size == 0) { - printf("Error: input file has 0 size\n"); + /* Check if it's a real error or just an empty file */ + if (fseek(in, 0, SEEK_END) != 0 || ftell(in) < 0) + printf("Error: Failed to get file size\n"); + else + printf("Error: Input file has zero length\n"); exit(1); } diff --git a/igzip/igzip_rand_test.c b/igzip/igzip_rand_test.c index 10eb0a4..4159eb9 100644 --- a/igzip/igzip_rand_test.c +++ b/igzip/igzip_rand_test.c @@ -2653,6 +2653,15 @@ test_compress_file(char *file_name) } in_size = get_filesize(in_file); + if (in_size == 0) { + /* Check if it's a real error or just an empty file */ + if (fseek(in_file, 0, SEEK_END) != 0 || ftell(in_file) < 0) + printf("Failed to get file size for %s\n", file_name); + else + printf("File %s has zero length\n", file_name); + ret = FILE_READ_FAILED; + goto exit_comp_file; + } if (in_size > MAX_FILE_SIZE) in_size = MAX_FILE_SIZE; diff --git a/include/test.h b/include/test.h index 1d4b56e..08a069a 100644 --- a/include/test.h +++ b/include/test.h @@ -401,15 +401,20 @@ get_filesize(FILE *fp) uint64_t file_size; fpos_t pos, pos_curr; - fgetpos(fp, &pos_curr); /* Save current position */ + if (fgetpos(fp, &pos_curr) != 0) /* Save current position */ + return 0; #if defined(_WIN32) || defined(_WIN64) - _fseeki64(fp, 0, SEEK_END); + if (_fseeki64(fp, 0, SEEK_END) != 0) + return 0; #else - fseeko(fp, 0, SEEK_END); + if (fseeko(fp, 0, SEEK_END) != 0) + return 0; #endif - fgetpos(fp, &pos); + if (fgetpos(fp, &pos) != 0) + return 0; file_size = *(uint64_t *) &pos; - fsetpos(fp, &pos_curr); /* Restore position */ + if (fsetpos(fp, &pos_curr) != 0) /* Restore position */ + return 0; return file_size; } diff --git a/programs/igzip_cli.c b/programs/igzip_cli.c index f85ef96..cd1379d 100644 --- a/programs/igzip_cli.c +++ b/programs/igzip_cli.c @@ -211,15 +211,20 @@ get_filesize(FILE *fp) size_t file_size; fpos_t pos, pos_curr; - fgetpos(fp, &pos_curr); /* Save current position */ + if (fgetpos(fp, &pos_curr) != 0) /* Save current position */ + return 0; #if defined(_WIN32) || defined(_WIN64) - _fseeki64(fp, 0, SEEK_END); + if (_fseeki64(fp, 0, SEEK_END) != 0) + return 0; #else - fseeko(fp, 0, SEEK_END); + if (fseeko(fp, 0, SEEK_END) != 0) + return 0; #endif - fgetpos(fp, &pos); + if (fgetpos(fp, &pos) != 0) + return 0; file_size = *(size_t *) &pos; - fsetpos(fp, &pos_curr); /* Restore position */ + if (fsetpos(fp, &pos_curr) != 0) /* Restore position */ + return 0; return file_size; } diff --git a/tests/fuzz/igzip_fuzz_inflate.c b/tests/fuzz/igzip_fuzz_inflate.c index bf23de2..7c07309 100644 --- a/tests/fuzz/igzip_fuzz_inflate.c +++ b/tests/fuzz/igzip_fuzz_inflate.c @@ -27,6 +27,14 @@ main(int argc, char *argv[]) exit(1); } in_file_size = get_filesize(in); + if (in_file_size == 0) { + /* Check if it's a real error or just an empty file */ + if (fseek(in, 0, SEEK_END) != 0 || ftell(in) < 0) + fprintf(stderr, "Failed to get file size for %s\n", argv[1]); + else + fprintf(stderr, "Input file %s has zero length\n", argv[1]); + exit(1); + } in_buf = malloc(in_file_size); if (in_buf == NULL) {