diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-01-05 12:12:19 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-05 12:12:19 +0000 |
commit | f0d7922c9bafff38e12978625f467aafebe75145 (patch) | |
tree | 055035c74116c9c3601491c07ab8a6b5fe8194d4 /src/posix.c | |
parent | fa0178608699ce23c942f719c47eaada645db409 (diff) | |
parent | 4fafe9f6a15ccf540d23c96d37ca1c3127a7ddac (diff) | |
download | libgit2-f0d7922c9bafff38e12978625f467aafebe75145.tar.gz |
Merge pull request #5583 from 0xdky/dhruva/build-with-nommap
Build with NO_MMAP
Diffstat (limited to 'src/posix.c')
-rw-r--r-- | src/posix.c | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/src/posix.c b/src/posix.c index 9cd96d2c8..bf764ae6b 100644 --- a/src/posix.c +++ b/src/posix.c @@ -238,24 +238,43 @@ int git__mmap_alignment(size_t *alignment) int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset) { + const char *ptr; + size_t remaining_len; + GIT_MMAP_VALIDATE(out, len, prot, flags); - out->data = NULL; - out->len = 0; + /* writes cannot be emulated without handling pagefaults since write happens by + * writing to mapped memory */ + if (prot & GIT_PROT_WRITE) { + git_error_set(GIT_ERROR_OS, "trying to map %s-writeable", + ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED) ? "shared": "private"); + return -1; + } - if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) { - git_error_set(GIT_ERROR_OS, "trying to map shared-writeable"); + if (!git__is_ssizet(len)) { + errno = EINVAL; return -1; } + out->len = 0; out->data = git__malloc(len); GIT_ERROR_CHECK_ALLOC(out->data); - if (!git__is_ssizet(len) || - (p_lseek(fd, offset, SEEK_SET) < 0) || - (p_read(fd, out->data, len) != (ssize_t)len)) { - git_error_set(GIT_ERROR_OS, "mmap emulation failed"); - return -1; + remaining_len = len; + ptr = (const char *)out->data; + while (remaining_len > 0) { + ssize_t nb; + HANDLE_EINTR(nb, p_pread(fd, (void *)ptr, remaining_len, offset)); + if (nb <= 0) { + git_error_set(GIT_ERROR_OS, "mmap emulation failed"); + git__free(out->data); + out->data = NULL; + return -1; + } + + ptr += nb; + offset += nb; + remaining_len -= nb; } out->len = len; @@ -267,6 +286,10 @@ int p_munmap(git_map *map) GIT_ASSERT_ARG(map); git__free(map->data); + /* Initializing will help debug use-after-free */ + map->len = 0; + map->data = NULL; + return 0; } |