From 3c3ed706908022ef26b5889971c4ec5040697f95 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Tue, 26 Jun 2012 13:48:36 +0200 Subject: lib/util: fix fd leak in anonymous_shared_allocate() if MAP_ANON is not available metze --- lib/util/util.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/util/util.c b/lib/util/util.c index 20466b41b3..100d3d84ab 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -1109,8 +1109,21 @@ void *anonymous_shared_allocate(size_t orig_bufsz) buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1 /* fd */, 0 /* offset */); #else +{ + int saved_errno; + int fd; + + fd = open("/dev/zero", O_RDWR); + if (fd == -1) { + return NULL; + } + buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, - open("/dev/zero", O_RDWR), 0 /* offset */); + fd, 0 /* offset */); + saved_errno = errno; + close(fd); + errno = saved_errno; +} #endif if (buf == MAP_FAILED) { -- cgit