slirp: tcp_listen(): Don't try to close() an fd we never opened
authorPeter Maydell <peter.maydell@linaro.org>
Sat, 4 Feb 2017 23:08:35 +0000 (23:08 +0000)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Sun, 26 Feb 2017 14:39:29 +0000 (15:39 +0100)
Coverity points out (CID 1005725) that an error-exit path in tcp_listen()
will try to close(s) even if the reason it got there was that the
qemu_socket() failed and s was never opened.  Not only that, this isn't even
the right function to use, because we need closesocket() to do the right
thing on Windows.  Change to using the right function and only calling it if
needed.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
slirp/socket.c

index 6c189713688cdf10ce25b01221d67a15e864ca0b..86927722e1abe9f3ea2d5cc2bccfc6bb284ce766 100644 (file)
@@ -713,7 +713,9 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
            (listen(s,1) < 0)) {
                int tmperrno = errno; /* Don't clobber the real reason we failed */
 
-               close(s);
+                if (s >= 0) {
+                    closesocket(s);
+                }
                sofree(so);
                /* Restore the real errno */
 #ifdef _WIN32