From: Christopher Dykes Date: Thu, 25 Aug 2016 21:52:05 +0000 (-0700) Subject: Allow locking nullptr if the length is 0 X-Git-Tag: v2016.08.29.00~9 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=19c6708bbcdc5e8f0241b567a1e55dd579d0304a;p=folly.git Allow locking nullptr if the length is 0 Summary: Because apparently this is valid (and we test it). This also adds a check in mmap to make sure we aren't passing an invalid handle to `MapViewOfFileEx`. Reviewed By: yfeldblum Differential Revision: D3772853 fbshipit-source-id: 11593997a3fb12b7b391c5e52661060b71341aef --- diff --git a/folly/portability/SysMman.cpp b/folly/portability/SysMman.cpp index 9827f045..38d56ae3 100755 --- a/folly/portability/SysMman.cpp +++ b/folly/portability/SysMman.cpp @@ -64,6 +64,13 @@ int madvise(const void* addr, size_t len, int advise) { } int mlock(const void* addr, size_t len) { + // For some strange reason, it's allowed to + // lock a nullptr as long as length is zero. + // VirtualLock doesn't allow it, so handle + // it specially. + if (addr == nullptr && len == 0) { + return 0; + } if (!VirtualLock((void*)addr, len)) { return -1; } @@ -106,6 +113,9 @@ void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) { (DWORD)((length >> 32) & 0xFFFFFFFF), (DWORD)(length & 0xFFFFFFFF), nullptr); + if (fmh == nullptr) { + return MAP_FAILED; + } ret = MapViewOfFileEx( fmh, accessFlags, @@ -148,6 +158,10 @@ int mprotect(void* addr, size_t size, int prot) { } int munlock(const void* addr, size_t length) { + // See comment in mlock + if (addr == nullptr && length == 0) { + return 0; + } if (!VirtualUnlock((void*)addr, length)) { return -1; } diff --git a/folly/portability/SysMman.h b/folly/portability/SysMman.h index 52dd33c8..907c6778 100755 --- a/folly/portability/SysMman.h +++ b/folly/portability/SysMman.h @@ -44,6 +44,7 @@ #define PROT_WRITE 2 #define PROT_EXEC 4 +#define MADV_NORMAL 0 #define MADV_DONTNEED 0 #define MADV_SEQUENTIAL 0