From: Michael Tokarev <mjt@tls.msk.ru>
To: arei.gonglei@huawei.com, qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org, peter.huangpeng@huawei.com, stefanha@redhat.com
Subject: Re: [Qemu-trivial] [PATCH 2/2] tap: fix possible fd leak
Date: Sun, 02 Nov 2014 08:11:02 +0300 [thread overview]
Message-ID: <5455BCE6.5080804@msgid.tls.msk.ru> (raw)
In-Reply-To: <1414735861-1232-3-git-send-email-arei.gonglei@huawei.com>
31.10.2014 09:11, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
>
> In hotplugging scenario, taking those true branch, the file
> handler do not be closed. Adding cleanup logic for them.
>
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
> net/tap.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/net/tap.c b/net/tap.c
> index 7bcd4c7..3cfbee8 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -796,7 +796,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
> if (net_init_tap_one(tap, peer, "bridge", name, ifname,
> script, downscript, vhostfdname,
> vnet_hdr, fd)) {
> - return -1;
> + goto fail;
> }
> } else {
> if (tap->has_vhostfds) {
> @@ -823,7 +823,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
> if (queues > 1 && i == 0 && !tap->has_ifname) {
> if (tap_fd_get_ifname(fd, ifname)) {
> error_report("Fail to get ifname");
> - return -1;
> + goto fail;
> }
> }
>
> @@ -831,12 +831,18 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
> i >= 1 ? "no" : script,
> i >= 1 ? "no" : downscript,
> vhostfdname, vnet_hdr, fd)) {
> - return -1;
> + goto fail;
> }
> }
> }
>
> return 0;
> +
> +fail:
> + if (fd != -1) {
> + close(fd);
> + }
> + return -1;
> }
I think, given the somewhat "hairy" nature of net_init_tap() function, which
has many error returns which should not close fd and just 3 which should, it
is better to add explicit close(fd) in these 3 places.
Besides, why do you check for fd != -1 in the fail path? You added the goto
into the 3 places, all of them has fd != -1, and there's no other ways to
reach this place. Are you not certain that fd will be valid here? If yes,
I think this is yet another argument for adding close()s into the 3 places.
Thanks,
/mjt
WARNING: multiple messages have this Message-ID (diff)
From: Michael Tokarev <mjt@tls.msk.ru>
To: arei.gonglei@huawei.com, qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org, peter.huangpeng@huawei.com, stefanha@redhat.com
Subject: Re: [Qemu-devel] [Qemu-trivial] [PATCH 2/2] tap: fix possible fd leak
Date: Sun, 02 Nov 2014 08:11:02 +0300 [thread overview]
Message-ID: <5455BCE6.5080804@msgid.tls.msk.ru> (raw)
In-Reply-To: <1414735861-1232-3-git-send-email-arei.gonglei@huawei.com>
31.10.2014 09:11, arei.gonglei@huawei.com wrote:
> From: Gonglei <arei.gonglei@huawei.com>
>
> In hotplugging scenario, taking those true branch, the file
> handler do not be closed. Adding cleanup logic for them.
>
> Signed-off-by: Gonglei <arei.gonglei@huawei.com>
> ---
> net/tap.c | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/net/tap.c b/net/tap.c
> index 7bcd4c7..3cfbee8 100644
> --- a/net/tap.c
> +++ b/net/tap.c
> @@ -796,7 +796,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
> if (net_init_tap_one(tap, peer, "bridge", name, ifname,
> script, downscript, vhostfdname,
> vnet_hdr, fd)) {
> - return -1;
> + goto fail;
> }
> } else {
> if (tap->has_vhostfds) {
> @@ -823,7 +823,7 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
> if (queues > 1 && i == 0 && !tap->has_ifname) {
> if (tap_fd_get_ifname(fd, ifname)) {
> error_report("Fail to get ifname");
> - return -1;
> + goto fail;
> }
> }
>
> @@ -831,12 +831,18 @@ int net_init_tap(const NetClientOptions *opts, const char *name,
> i >= 1 ? "no" : script,
> i >= 1 ? "no" : downscript,
> vhostfdname, vnet_hdr, fd)) {
> - return -1;
> + goto fail;
> }
> }
> }
>
> return 0;
> +
> +fail:
> + if (fd != -1) {
> + close(fd);
> + }
> + return -1;
> }
I think, given the somewhat "hairy" nature of net_init_tap() function, which
has many error returns which should not close fd and just 3 which should, it
is better to add explicit close(fd) in these 3 places.
Besides, why do you check for fd != -1 in the fail path? You added the goto
into the 3 places, all of them has fd != -1, and there's no other ways to
reach this place. Are you not certain that fd will be valid here? If yes,
I think this is yet another argument for adding close()s into the 3 places.
Thanks,
/mjt
next prev parent reply other threads:[~2014-11-02 5:11 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-31 6:10 [Qemu-trivial] [PATCH 0/2] tap: add cleanup logic avoiding leaking fd arei.gonglei
2014-10-31 6:10 ` [Qemu-devel] " arei.gonglei
2014-10-31 6:11 ` [Qemu-trivial] [PATCH 1/2] tap: remove close(fd) arei.gonglei
2014-10-31 6:11 ` [Qemu-devel] " arei.gonglei
2014-11-02 5:06 ` [Qemu-trivial] " Michael Tokarev
2014-11-02 5:06 ` [Qemu-devel] " Michael Tokarev
2014-11-02 5:09 ` Gonglei
2014-11-02 5:09 ` [Qemu-devel] " Gonglei
2014-10-31 6:11 ` [Qemu-trivial] [PATCH 2/2] tap: fix possible fd leak arei.gonglei
2014-10-31 6:11 ` [Qemu-devel] " arei.gonglei
2014-11-02 5:11 ` Michael Tokarev [this message]
2014-11-02 5:11 ` [Qemu-devel] [Qemu-trivial] " Michael Tokarev
2014-11-02 5:21 ` Gonglei
2014-11-02 5:21 ` [Qemu-devel] " Gonglei
2014-11-02 5:24 ` Michael Tokarev
2014-11-02 5:24 ` [Qemu-devel] " Michael Tokarev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5455BCE6.5080804@msgid.tls.msk.ru \
--to=mjt@tls.msk.ru \
--cc=arei.gonglei@huawei.com \
--cc=peter.huangpeng@huawei.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-trivial@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.