Skip to content

Commit c3d9d16

Browse files
author
nnposter
committed
Fix file size arithmetic on Win32. Closes nmap#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.
1 parent 67a04de commit c3d9d16

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

utils.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ static HANDLE gmap = NULL;
595595
char *mmapfile(char *fname, s64 *length, int openflags) {
596596
HANDLE fd;
597597
DWORD mflags, oflags;
598-
DWORD lowsize, highsize;
598+
LARGE_INTEGER filesize;
599599
char *fileptr;
600600

601601
if (!length || !fname) {
@@ -622,11 +622,10 @@ char *mmapfile(char *fname, s64 *length, int openflags) {
622622
if (!fd)
623623
pfatal ("%s(%u): CreateFile()", __FILE__, __LINE__);
624624

625-
lowsize = GetFileSize (fd, &highsize);
626-
if (lowsize == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
627-
pfatal("%s(%u): GetFileSize(), file '%s'", __FILE__, __LINE__, fname);
625+
if (!GetFileSizeEx(fd, &filesize)) {
626+
pfatal("%s(%u): GetFileSizeEx(), file '%s'", __FILE__, __LINE__, fname);
628627
}
629-
*length = lowsize + highsize << sizeof(DWORD);
628+
*length = (s64)filesize.QuadPart;
630629
if (*length < 0) {
631630
fatal("%s(%u): size too large, file '%s'", __FILE__, __LINE__, fname);
632631
}

0 commit comments

Comments
 (0)