diff mbox

net: Fix memory leak in net_param_nic()

Message ID 1525073205-22394-1-git-send-email-thuth@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Thomas Huth April 30, 2018, 7:26 a.m. UTC
The early exits in case of errors leak the memory allocated for nd_id.
Fix it by using a "goto out" to the cleanup at the end of the function
instead.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 net/net.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Peter Maydell April 30, 2018, 7:51 a.m. UTC | #1
On 30 April 2018 at 08:26, Thomas Huth <thuth@redhat.com> wrote:
> The early exits in case of errors leak the memory allocated for nd_id.
> Fix it by using a "goto out" to the cleanup at the end of the function
> instead.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---

Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM
Philippe Mathieu-Daudé May 1, 2018, 3:20 a.m. UTC | #2
On 04/30/2018 04:26 AM, Thomas Huth wrote:
> The early exits in case of errors leak the memory allocated for nd_id.
> Fix it by using a "goto out" to the cleanup at the end of the function
> instead.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
>  net/net.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/net/net.c b/net/net.c
> index 29f8398..65457b7 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -1502,11 +1502,12 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
>          g_free(mac);
>          if (ret) {
>              error_setg(errp, "invalid syntax for ethernet address");
> -            return -1;
> +            goto out;
>          }
>          if (is_multicast_ether_addr(ni->macaddr.a)) {
>              error_setg(errp, "NIC cannot have multicast MAC address");
> -            return -1;
> +            ret = -1;
> +            goto out;
>          }
>      }
>      qemu_macaddr_default_if_unset(&ni->macaddr);
> @@ -1518,6 +1519,7 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
>          nb_nics++;
>      }
>  
> +out:
>      g_free(nd_id);
>      return ret;
>  }
>
Jason Wang May 2, 2018, 2:20 a.m. UTC | #3
On 2018年04月30日 15:26, Thomas Huth wrote:
> The early exits in case of errors leak the memory allocated for nd_id.
> Fix it by using a "goto out" to the cleanup at the end of the function
> instead.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>   net/net.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/net/net.c b/net/net.c
> index 29f8398..65457b7 100644
> --- a/net/net.c
> +++ b/net/net.c
> @@ -1502,11 +1502,12 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
>           g_free(mac);
>           if (ret) {
>               error_setg(errp, "invalid syntax for ethernet address");
> -            return -1;
> +            goto out;
>           }
>           if (is_multicast_ether_addr(ni->macaddr.a)) {
>               error_setg(errp, "NIC cannot have multicast MAC address");
> -            return -1;
> +            ret = -1;
> +            goto out;
>           }
>       }
>       qemu_macaddr_default_if_unset(&ni->macaddr);
> @@ -1518,6 +1519,7 @@ static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
>           nb_nics++;
>       }
>   
> +out:
>       g_free(nd_id);
>       return ret;
>   }

Applied, thanks.
diff mbox

Patch

diff --git a/net/net.c b/net/net.c
index 29f8398..65457b7 100644
--- a/net/net.c
+++ b/net/net.c
@@ -1502,11 +1502,12 @@  static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
         g_free(mac);
         if (ret) {
             error_setg(errp, "invalid syntax for ethernet address");
-            return -1;
+            goto out;
         }
         if (is_multicast_ether_addr(ni->macaddr.a)) {
             error_setg(errp, "NIC cannot have multicast MAC address");
-            return -1;
+            ret = -1;
+            goto out;
         }
     }
     qemu_macaddr_default_if_unset(&ni->macaddr);
@@ -1518,6 +1519,7 @@  static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
         nb_nics++;
     }
 
+out:
     g_free(nd_id);
     return ret;
 }