From: Michal Privoznik <[email protected]>

Inside of bhyveBuildNetArgStr() there is @nic_model which is
allocated, appended into cmd line and then freed under cleanup
label. Firstly, There are few cases where instead of jumping onto
the label there's a return statement (this alone can lead to a
memory leak), but more importantly - the variable doesn't need
dynamically allocated string. It's the same story with @brname.
After making them both const strings, the return statements can
be used more freely (up until first possible allocation).

6 bytes in 1 blocks are definitely lost in loss record 4 of 508
   at 0x4883224: malloc (vg_replace_malloc.c:451)
   by 0x4EE6562: g_malloc (in /usr/local/lib/libglib-2.0.so.0.8400.4)
   by 0x4F0100F: g_strdup (in /usr/local/lib/libglib-2.0.so.0.8400.4)
   by 0x401BC02: g_strdup_inline (gstrfuncs.h:321)
   by 0x401BC02: bhyveBuildNetArgStr (bhyve_command.c:64)
   by 0x401B362: virBhyveProcessBuildBhyveCmd (bhyve_command.c:1033)
   by 0x4015F15: testCompareXMLToArgvFiles (bhyvexml2argvtest.c:72)
   by 0x4015BB9: testCompareXMLToArgvHelper (bhyvexml2argvtest.c:144)
   by 0x4016598: virTestRun (testutils.c:143)
   by 0x4015121: mymain (bhyvexml2argvtest.c:275)
   by 0x4018892: virTestMain (testutils.c:913)
   by 0x4013DC6: main (bhyvexml2argvtest.c:352)

Signed-off-by: Michal Privoznik <[email protected]>
---
 src/bhyve/bhyve_command.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c
index c9bfe22c8c..ff079da9ef 100644
--- a/src/bhyve/bhyve_command.c
+++ b/src/bhyve/bhyve_command.c
@@ -51,17 +51,17 @@ bhyveBuildNetArgStr(const virDomainDef *def,
 {
     char macaddr[VIR_MAC_STRING_BUFLEN];
     char *realifname = NULL;
-    char *brname = NULL;
-    char *nic_model = NULL;
+    const char *brname = NULL;
+    const char *nic_model = NULL;
     int ret = -1;
     virDomainNetType actualType = virDomainNetGetActualType(net);
     g_autoptr(virConnect) netconn = NULL;
 
     if (net->model == VIR_DOMAIN_NET_MODEL_VIRTIO) {
-        nic_model = g_strdup("virtio-net");
+        nic_model = "virtio-net";
     } else if (net->model == VIR_DOMAIN_NET_MODEL_E1000) {
         if ((bhyveDriverGetBhyveCaps(driver) & BHYVE_CAP_NET_E1000) != 0) {
-            nic_model = g_strdup("e1000");
+            nic_model = "e1000";
         } else {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("NIC model 'e1000' is not supported by given 
bhyve binary"));
@@ -75,9 +75,9 @@ bhyveBuildNetArgStr(const virDomainDef *def,
 
     if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK) {
         if (!netconn && !(netconn = virGetConnectNetwork()))
-            goto cleanup;
+            return -1;
         if (virDomainNetAllocateActualDevice(netconn, def, net) < 0)
-            goto cleanup;
+            return -1;
     }
     /* final validation now that actual type is known */
     if (virDomainActualNetDefValidate(net) < 0)
@@ -88,11 +88,11 @@ bhyveBuildNetArgStr(const virDomainDef *def,
     switch (actualType) {
     case VIR_DOMAIN_NET_TYPE_NETWORK:
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
-        brname = g_strdup(virDomainNetGetActualBridgeName(net));
+        brname = virDomainNetGetActualBridgeName(net);
         if (!brname) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("No bridge name specified"));
-            goto cleanup;
+            return -1;
         }
         break;
     case VIR_DOMAIN_NET_TYPE_USER:
@@ -116,7 +116,7 @@ bhyveBuildNetArgStr(const virDomainDef *def,
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("Unsupported network type %1$s"),
                        virDomainNetTypeToString(actualType));
-        goto cleanup;
+        return -1;
     }
 
     if (!dryRun) {
@@ -156,9 +156,7 @@ bhyveBuildNetArgStr(const virDomainDef *def,
  cleanup:
     if (ret < 0)
         VIR_FREE(net->ifname);
-    VIR_FREE(brname);
     VIR_FREE(realifname);
-    VIR_FREE(nic_model);
 
     return ret;
 }
-- 
2.52.0

Reply via email to