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

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