]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[1705] Don't leak the fd on close error
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>
Tue, 28 Feb 2012 18:05:10 +0000 (19:05 +0100)
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>
Tue, 28 Feb 2012 18:05:10 +0000 (19:05 +0100)
src/lib/util/io/fd_share.cc

index 2d6c338760992ca18adc415979b60757fb7d864f..7adbbbe9fa362fef9067ed1866c31a4224dd93d9 100644 (file)
@@ -111,7 +111,14 @@ recv_fd(const int sock) {
     // one returned previously, even if that one is not closed yet. So,
     // we just re-number every one we get, so they are unique.
     int new_fd(dup(fd));
-    if (close(fd) == -1 || new_fd == -1) {
+    int close_error(close(fd));
+    if (close_error == -1 || new_fd == -1) {
+        // We need to return an error, because something failed. But in case
+        // it was the previous close, we at least try to close the duped FD.
+        if (new_fd != -1) {
+            close(new_fd); // If this fails, nothing but returning error can't
+                           // be done and we are doing that anyway.
+        }
         return (FD_SYSTEM_ERROR);
     }
     return (new_fd);