aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Cherry <tomcherry@google.com>2018-02-20 15:50:04 -0800
committerTom Cherry <tomcherry@google.com>2018-02-20 15:50:04 -0800
commitcb4d42173e008217ecb75ef7669d1c41be6ae0c7 (patch)
tree89b605ab10eaecfeed80a39389a95b053c66ae87
parentd5172fced0f88114e21086db5defaee64371de32 (diff)
downloadbionic-cb4d42173e008217ecb75ef7669d1c41be6ae0c7.tar.gz
Fix mmap leak in MmapFile
If the mmap'ed file doesn't end in a new line, previously we'd leak the mmap'ed region. This change now munmap's the region. Test: unit tests Change-Id: If28d3d9a6b1b9c54123beecb3bbbe8ed984ca81d
-rw-r--r--libc/bionic/grp_pwd_file.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/libc/bionic/grp_pwd_file.cpp b/libc/bionic/grp_pwd_file.cpp
index 911daeadb..4b8b8a99a 100644
--- a/libc/bionic/grp_pwd_file.cpp
+++ b/libc/bionic/grp_pwd_file.cpp
@@ -233,7 +233,7 @@ bool MmapFile::DoMmap() {
auto mmap_size = fd_stat.st_size;
- const void* map_result = mmap(nullptr, mmap_size, PROT_READ, MAP_SHARED, fd, 0);
+ void* map_result = mmap(nullptr, mmap_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
if (map_result == MAP_FAILED) {
@@ -243,7 +243,12 @@ bool MmapFile::DoMmap() {
start_ = static_cast<const char*>(map_result);
end_ = start_ + mmap_size - 1;
- return *end_ == '\n';
+ if (*end_ != '\n') {
+ munmap(map_result, mmap_size);
+ return false;
+ }
+
+ return true;
}
template <typename Line, typename Predicate>