don't try to mmap 0-sized things (#26)

This commit is contained in:
Ethan Roseman 2022-09-25 02:03:40 +09:00 committed by GitHub
parent b65a585ee3
commit 681137902b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 4 deletions

View File

@ -405,8 +405,10 @@ namespace kernel32 {
fclose(fp); fclose(fp);
} }
} else if (data.type == handles::TYPE_MAPPED) { } else if (data.type == handles::TYPE_MAPPED) {
if (data.ptr != (void *) 0x1) {
munmap(data.ptr, data.size); munmap(data.ptr, data.size);
} }
}
return 1; return 1;
} }
@ -665,7 +667,11 @@ namespace kernel32 {
void *mmapped; void *mmapped;
if (hFile == (void*) -1) { // INVALID_HANDLE_VALUE if (hFile == (void*) -1) { // INVALID_HANDLE_VALUE
if (size == 0) {
mmapped = (void *) 0x1;
} else {
mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); mmapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
}
} else { } else {
int fd = fileno(files::fpFromHandle(hFile)); int fd = fileno(files::fpFromHandle(hFile));
@ -675,8 +681,13 @@ namespace kernel32 {
return (void*) -1; return (void*) -1;
} }
} }
if (size == 0) {
mmapped = (void *) 0x1;
} else {
mmapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); mmapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
} }
}
assert(mmapped != MAP_FAILED); assert(mmapped != MAP_FAILED);
return handles::allocDataHandle({handles::TYPE_MAPPED, mmapped, (unsigned int) size}); return handles::allocDataHandle({handles::TYPE_MAPPED, mmapped, (unsigned int) size});