lib/util: fix fd leak in anonymous_shared_allocate() if MAP_ANON is not available
authorStefan Metzmacher <metze@samba.org>
Tue, 26 Jun 2012 11:48:36 +0000 (13:48 +0200)
committerMichael Adam <obnox@samba.org>
Fri, 29 Jun 2012 13:21:10 +0000 (15:21 +0200)
metze

lib/util/util.c

index 20466b41b3c04f2333f6f25be4563af0c454e314..100d3d84ab377c044ef3887bc658ccbea1931227 100644 (file)
@@ -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) {