From 681137902b54e3c8f9875fc79f4f9ac1d072f1ec Mon Sep 17 00:00:00 2001 From: Ethan Roseman Date: Sun, 25 Sep 2022 02:03:40 +0900 Subject: [PATCH] don't try to mmap 0-sized things (#26) --- dll/kernel32.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/dll/kernel32.cpp b/dll/kernel32.cpp index 95f4846..dae41df 100644 --- a/dll/kernel32.cpp +++ b/dll/kernel32.cpp @@ -405,7 +405,9 @@ namespace kernel32 { fclose(fp); } } else if (data.type == handles::TYPE_MAPPED) { - munmap(data.ptr, data.size); + if (data.ptr != (void *) 0x1) { + munmap(data.ptr, data.size); + } } return 1; } @@ -662,10 +664,14 @@ namespace kernel32 { int64_t size = (int64_t) dwMaximumSizeHigh << 32 | dwMaximumSizeLow; - void* mmapped; + void *mmapped; if (hFile == (void*) -1) { // INVALID_HANDLE_VALUE - mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (size == 0) { + mmapped = (void *) 0x1; + } else { + mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + } } else { int fd = fileno(files::fpFromHandle(hFile)); @@ -675,7 +681,12 @@ namespace kernel32 { return (void*) -1; } } - mmapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + + if (size == 0) { + mmapped = (void *) 0x1; + } else { + mmapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + } } assert(mmapped != MAP_FAILED);