From c3d9d16744dc445f14ea5504dcde12e85a715107 Mon Sep 17 00:00:00 2001 From: nnposter Date: Tue, 18 May 2021 03:15:22 +0000 Subject: [PATCH] Fix file size arithmetic on Win32. Closes #2306 The old code was incorrectly calculating sizes of files exceeding 4 GB. The new code skips the arithmetic altogether by using a different API. --- utils.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/utils.cc b/utils.cc index b6717e31c..4b719bc20 100644 --- a/utils.cc +++ b/utils.cc @@ -595,7 +595,7 @@ static HANDLE gmap = NULL; char *mmapfile(char *fname, s64 *length, int openflags) { HANDLE fd; DWORD mflags, oflags; - DWORD lowsize, highsize; + LARGE_INTEGER filesize; char *fileptr; if (!length || !fname) { @@ -622,11 +622,10 @@ char *mmapfile(char *fname, s64 *length, int openflags) { if (!fd) pfatal ("%s(%u): CreateFile()", __FILE__, __LINE__); - lowsize = GetFileSize (fd, &highsize); - if (lowsize == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) { - pfatal("%s(%u): GetFileSize(), file '%s'", __FILE__, __LINE__, fname); + if (!GetFileSizeEx(fd, &filesize)) { + pfatal("%s(%u): GetFileSizeEx(), file '%s'", __FILE__, __LINE__, fname); } - *length = lowsize + highsize << sizeof(DWORD); + *length = (s64)filesize.QuadPart; if (*length < 0) { fatal("%s(%u): size too large, file '%s'", __FILE__, __LINE__, fname); }