From 85a79a1c36204777b050b31fd647679d2d17985f Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Thu, 15 Oct 2026 02:49:06 +0200 Subject: [PATCH] Windows: do not truncate files of 4 GiB or more in LoadFile LoadFile read the file size with GetFileSize without its high-order part, so a file of 4 GiB or more was loaded as if it were only as large as its size modulo 4 GiB, without any error. Refuse such files with ERROR_FILE_TOO_LARGE instead. --- src/Common/Dlgcode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 3f3dec56..94121ec7 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -523,14 +523,18 @@ char *LoadFile (const wchar_t *fileName, DWORD *size) { char *buf; DWORD fileSize = INVALID_FILE_SIZE; + DWORD fileSizeHigh = 0; HANDLE h = CreateFile (fileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); *size = 0; if (h == INVALID_HANDLE_VALUE) return NULL; - if ((fileSize = GetFileSize (h, NULL)) == INVALID_FILE_SIZE) + if ((fileSize = GetFileSize (h, &fileSizeHigh)) == INVALID_FILE_SIZE || fileSizeHigh != 0) { CloseHandle (h); + // Refuse a file of 4 GiB or more, which would otherwise be truncated to its size modulo 4 GiB + if (fileSizeHigh != 0) + SetLastError (ERROR_FILE_TOO_LARGE); return NULL; }