[PATCH] net/tap: Avoid "close(-1)" in net_init_tap() error path

Peter Maydell posted 1 patch 1 day, 4 hours ago
net/tap.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] net/tap: Avoid "close(-1)" in net_init_tap() error path
Posted by Peter Maydell 1 day, 4 hours ago
Coverity point out (CID 1660042, 1660052) that some error-exit paths
in net_init_tap() reach the "close(fd)" at the 'fail' label when 'fd'
still has its initial value of -1.  This isn't a valid fd, so while
close(-1) will fail and we are ignoring its return value anyway, we
should avoid it.  (Compare net_free_fds() which has "don't close -1"
logic in it.)

Resolves: Coverity CID 1660042, 1660052
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 net/tap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/tap.c b/net/tap.c
index d71e3dd1175..ea0ad084819 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -1245,7 +1245,9 @@ int net_init_tap(const Netdev *netdev, const char *name,
     return 0;
 
 fail:
-    close(fd);
+    if (fd != -1) {
+        close(fd);
+    }
     net_free_fds(fds, queues);
     net_free_fds(vhost_fds, queues);
     return -1;
-- 
2.43.0
Re: [PATCH] net/tap: Avoid "close(-1)" in net_init_tap() error path
Posted by Philippe Mathieu-Daudé 1 day, 4 hours ago
On 22/10/26 18:04, Peter Maydell wrote:
> Coverity point out (CID 1660042, 1660052) that some error-exit paths
> in net_init_tap() reach the "close(fd)" at the 'fail' label when 'fd'
> still has its initial value of -1.  This isn't a valid fd, so while
> close(-1) will fail and we are ignoring its return value anyway, we
> should avoid it.  (Compare net_free_fds() which has "don't close -1"
> logic in it.)
> 
> Resolves: Coverity CID 1660042, 1660052
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   net/tap.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>