diff mbox series

[1/1] net/socket.c: Fix memory leak

Message ID 20201209120013.13317-1-ruc_gongyuanjun@163.com (mailing list archive)
State New, archived
Headers show
Series [1/1] net/socket.c: Fix memory leak | expand

Commit Message

Yuanjun Gong Dec. 9, 2020, noon UTC
From: yuanjungong <ruc_gongyuanjun@163.com>

close fd opened by monitor_fd_param() before returning.

Signed-off-by: yuanjungong <ruc_gongyuanjun@163.com>
---
 net/socket.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Stefano Garzarella Dec. 10, 2020, 8:51 a.m. UTC | #1
On Wed, Dec 09, 2020 at 08:00:13PM +0800, ruc_gongyuanjun@163.com wrote:
>From: yuanjungong <ruc_gongyuanjun@163.com>
>
>close fd opened by monitor_fd_param() before returning.
>
>Signed-off-by: yuanjungong <ruc_gongyuanjun@163.com>
>---
> net/socket.c | 3 +++
> 1 file changed, 3 insertions(+)
>
>diff --git a/net/socket.c b/net/socket.c
>index 15b410e..2f720a7 100644
>--- a/net/socket.c
>+++ b/net/socket.c
>@@ -735,12 +735,15 @@ int net_init_socket(const Netdev *netdev, const char *name,
>         if (ret < 0) {
>             error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
>                              name, fd);
>+            close(fd);
>             return -1;
>         }
>         if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast,
>                                 errp)) {
>+            close(fd);

net_socket_fd_init() closes the fd in the error path, so this close 
should be avoided.

>             return -1;
>         }
>+        close(fd);

I don't know the code, but is it right to close the file descriptor in 
the success path?

Thanks,
Stefano

>         return 0;
>     }
>
>-- 
>2.17.1
>
>
diff mbox series

Patch

diff --git a/net/socket.c b/net/socket.c
index 15b410e..2f720a7 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -735,12 +735,15 @@  int net_init_socket(const Netdev *netdev, const char *name,
         if (ret < 0) {
             error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
                              name, fd);
+            close(fd);
             return -1;
         }
         if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast,
                                 errp)) {
+            close(fd);
             return -1;
         }
+        close(fd);
         return 0;
     }