From b915e4e11b666b72607b8c22044f5e20d9107cdd Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 22 Feb 2022 20:45:50 +1100 Subject: [PATCH] Close MappedFile fd using on_scope_end to handle all return paths --- src/file.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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()