Added SEEK error checking

This commit is contained in:
Pünkösd Marcell 2020-11-06 02:52:19 +01:00
parent 3d5729f7b7
commit 07d311628f
2 changed files with 11 additions and 3 deletions

View File

@ -23,7 +23,11 @@ uint8_t read_file_to_mem(mm_ctx context, const char *filename, uint64_t max_len,
return FILE_READ_COULD_NOT_OPEN; return FILE_READ_COULD_NOT_OPEN;
} }
fseek(fp, 0L, SEEK_END); if (fseek(fp, 0L, SEEK_END) != 0) {
fclose(fp);
return FILE_SEEK_ERROR;
}
uint64_t fsize = ftell(fp); uint64_t fsize = ftell(fp);
if (fsize > max_len) { if (fsize > max_len) {
@ -31,7 +35,10 @@ uint8_t read_file_to_mem(mm_ctx context, const char *filename, uint64_t max_len,
return FILE_READ_TOO_BIG; return FILE_READ_TOO_BIG;
} }
fseek(fp, 0L, SEEK_SET); if (fseek(fp, 0L, SEEK_SET) != 0) {
fclose(fp);
return FILE_SEEK_ERROR;
}
uint8_t *contents = (uint8_t *) magic_malloc(context, fsize); uint8_t *contents = (uint8_t *) magic_malloc(context, fsize);

View File

@ -14,7 +14,8 @@
#define FILE_READ_COULD_NOT_OPEN (FILE_READ_ERROR_PREFIX + 0x01) #define FILE_READ_COULD_NOT_OPEN (FILE_READ_ERROR_PREFIX + 0x01)
#define FILE_READ_TOO_BIG (FILE_READ_ERROR_PREFIX + 0x02) #define FILE_READ_TOO_BIG (FILE_READ_ERROR_PREFIX + 0x02)
#define FILE_READ_COULD_NOT_ALLOCATE (FILE_READ_ERROR_PREFIX + 0x03) #define FILE_READ_COULD_NOT_ALLOCATE (FILE_READ_ERROR_PREFIX + 0x03)
#define FILE_READ_ERROR (FILE_READ_ERROR_PREFIX + 0x04) #define FILE_SEEK_ERROR (FILE_READ_ERROR_PREFIX + 0x04)
#define FILE_READ_ERROR (FILE_READ_ERROR_PREFIX + 0x05)
#define FILE_READ_SUCCESS 0x00 #define FILE_READ_SUCCESS 0x00