fix: use 64-bit seek/tell in fpack, check ftell errors
This commit is contained in:
+19
-4
@@ -1,3 +1,8 @@
|
||||
#if !defined(_WIN32)
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#endif
|
||||
|
||||
#include "fpk.h"
|
||||
#include <dirent.h>
|
||||
#include <getopt.h>
|
||||
@@ -8,8 +13,12 @@
|
||||
|
||||
#ifdef _WIN32
|
||||
#define PROGRAM_NAME "fpack.exe"
|
||||
#define fpack_fseek64(f, off, whence) _fseeki64((f), (off), (whence))
|
||||
#define fpack_ftell64(f) _ftelli64((f))
|
||||
#else
|
||||
#define PROGRAM_NAME "fpack"
|
||||
#define fpack_fseek64(f, off, whence) fseeko((f), (off_t)(off), (whence))
|
||||
#define fpack_ftell64(f) ftello((f))
|
||||
#endif
|
||||
|
||||
#define FPACK_QUIET (1u << 0)
|
||||
@@ -92,16 +101,22 @@ static void normalize_path(char *s)
|
||||
static void *read_file(const char *path, size_t *out_size)
|
||||
{
|
||||
FILE *fp;
|
||||
long len;
|
||||
int64_t len;
|
||||
void *buf;
|
||||
|
||||
fp = fopen(path, "rb");
|
||||
if (!fp)
|
||||
return NULL;
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
len = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
if (fpack_fseek64(fp, 0, SEEK_END) != 0) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
len = fpack_ftell64(fp);
|
||||
if (len < 0 || fpack_fseek64(fp, 0, SEEK_SET) != 0) {
|
||||
fclose(fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf = malloc((size_t)len);
|
||||
if (!buf) {
|
||||
|
||||
Reference in New Issue
Block a user