Skip to contentSkip to conversation

net: Fix memory leak in net_param_nic()

PATCH Applied Thomas Huth 4 participants 4 messages Apr 30, 2018 – May 2, 2018 Networking

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: Fix memory leak in net_param_nic()

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.

---
 net/net.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/net.c +4 -2
index 29f8398..65457b7 100644
@@ -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;
 }
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Thomas Huth
Quoted 5 lines · Thomas Huth

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.

Quoted 32 lines
---
 net/net.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/net.c +4 -2
index 29f8398..65457b7 100644
@@ -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
#3 lore raw
In reply to Thomas Huth
Quoted 29 lines · Thomas Huth

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.

---
  net/net.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
net/net.c +3 -2
index 29f8398..65457b7 100644
@@ -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);

net/net.c static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) · line 1525

Quoted 7 lines
net/net.c +1 -0
@@ -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.