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.
This commit is contained in:
Mounir IDRASSI
2026-10-15 07:57:12 +02:00
parent 204d663952
commit 85a79a1c36
+5 -1
View File
@@ -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;
}