From f83ebd4266b57b3cae079fa69ce9693e03fa7472 Mon Sep 17 00:00:00 2001 From: Bertold Van den Bergh Date: Tue, 13 Mar 2018 10:54:25 +0100 Subject: [PATCH] Do not leak file descriptors --- library/zonedetect.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/library/zonedetect.c b/library/zonedetect.c index 61602bb..74bb8f4 100644 --- a/library/zonedetect.c +++ b/library/zonedetect.c @@ -371,6 +371,9 @@ void ZDCloseDatabase(ZoneDetect* library) { if(library->mapping) { munmap(library->mapping, library->length); } + if(library->fd >= 0) { + close(library->fd); + } free(library); } } @@ -381,18 +384,18 @@ ZoneDetect* ZDOpenDatabase(const char* path) { if(library) { memset(library, 0, sizeof(*library)); - int fd = open(path, O_RDONLY | O_CLOEXEC); - if(fd < 0) { + library->fd = open(path, O_RDONLY | O_CLOEXEC); + if(library->fd < 0) { goto fail; } - library->length = lseek(fd, 0, SEEK_END); + library->length = lseek(library->fd, 0, SEEK_END); if(library->length <= 0) { goto fail; } - lseek(fd, 0, SEEK_SET); + lseek(library->fd, 0, SEEK_SET); - library->mapping = mmap(NULL, library->length, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, 0); + library->mapping = mmap(NULL, library->length, PROT_READ, MAP_PRIVATE | MAP_FILE, library->fd, 0); if(!library->mapping) { goto fail; }