From 5f4c38a0e432c0f623c64d4c81977ff3d0ddf796 Mon Sep 17 00:00:00 2001 From: anth64 Date: Tue, 15 Sep 2026 11:00:24 +0200 Subject: [PATCH] fix: use 64-bit seek/tell in fpack, check ftell errors --- tools/fpack.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tools/fpack.c b/tools/fpack.c index 84dd837..717b5c3 100644 --- a/tools/fpack.c +++ b/tools/fpack.c @@ -1,3 +1,8 @@ +#if !defined(_WIN32) +#define _FILE_OFFSET_BITS 64 +#define _POSIX_C_SOURCE 200809L +#endif + #include "fpk.h" #include #include @@ -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) {