summaryrefslogtreecommitdiff
path: root/src/file.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2022-02-22 20:45:50 +1100
committerMaxime Coste <mawww@kakoune.org>2022-03-06 10:13:14 +1100
commitb915e4e11b666b72607b8c22044f5e20d9107cdd (patch)
treeb58d9bb5a71da71d562e91c4b3066f451997b988 /src/file.cc
parent30c05e83f8cbd0e116b9f8652f1cd2ade95add80 (diff)
Close MappedFile fd using on_scope_end to handle all return paths
Diffstat (limited to 'src/file.cc')
-rw-r--r--src/file.cc2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/file.cc b/src/file.cc
index a3a72ccc..e5d330be 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -211,6 +211,7 @@ MappedFile::MappedFile(StringView filename)
int fd = open(filename.zstr(), O_RDONLY | O_NONBLOCK);
if (fd == -1)
throw file_access_error(filename, strerror(errno));
+ auto close_fd = on_scope_end([&] { close(fd); });
fstat(fd, &st);
if (S_ISDIR(st.st_mode))
@@ -222,7 +223,6 @@ MappedFile::MappedFile(StringView filename)
data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (data == MAP_FAILED)
throw file_access_error{filename, strerror(errno)};
- close(fd);
}
MappedFile::~MappedFile()