Add error checking to get_filesize function

Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
This commit is contained in:
Pablo de Lara
2025-09-23 10:06:41 +00:00
parent bfc99b6a18
commit 0b3ec4f3b6
6 changed files with 61 additions and 12 deletions

View File

@@ -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");

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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) {