]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
nodedevmdevctltest: Fix two memleaks
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 26 Feb 2024 11:42:09 +0000 (12:42 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Mon, 26 Feb 2024 13:35:52 +0000 (14:35 +0100)
There are two memleaks inside of nodedevmdevctltest:

1) In the testCommandDryRunCallback() - when appending lines to
   stdinbuf the pointer is overwritten without freeing the old
   memory it pointed to.

2) In testMdevctlModify() the livecmd variable is reused and
   since its marked as g_autoptr() the first use leaks.

Fixes: 582f27ff15f14fa0e913278c0a2ca9840c3f12f5
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
tests/nodedevmdevctltest.c

index de688c982eb70a89daaaf6973f6a88debf1e6540..93b0efcb1cc947fd1f0b8bfd4b02a51f58048e4a 100644 (file)
@@ -33,10 +33,12 @@ testCommandDryRunCallback(const char *const*args G_GNUC_UNUSED,
 {
     char **stdinbuf = opaque;
 
-    if (*stdinbuf)
-        *stdinbuf = g_strconcat(*stdinbuf, "\n", input, NULL);
-    else
+    if (*stdinbuf) {
+        g_autofree char *oldbuf = g_steal_pointer(stdinbuf);
+        *stdinbuf = g_strconcat(oldbuf, "\n", input, NULL);
+    } else {
         *stdinbuf = g_strdup(input);
+    }
 }
 
 typedef virCommand * (*MdevctlCmdFunc)(virNodeDeviceDef *, char **, char **);
@@ -188,6 +190,7 @@ testMdevctlModify(const void *data G_GNUC_UNUSED)
     int ret = -1;
     g_autoptr(virCommand) definedcmd = NULL;
     g_autoptr(virCommand) livecmd = NULL;
+    g_autoptr(virCommand) livecmd_update = NULL;
     g_autoptr(virCommand) bothcmd = NULL;
     g_autofree char *errmsg = NULL;
     g_autofree char *stdinbuf = NULL;
@@ -222,10 +225,10 @@ testMdevctlModify(const void *data G_GNUC_UNUSED)
                                              &parser_callbacks, NULL, false)))
         goto cleanup;
 
-    if (!(livecmd = nodeDeviceGetMdevctlModifyCommand(def_update, false, true, &errmsg)))
+    if (!(livecmd_update = nodeDeviceGetMdevctlModifyCommand(def_update, false, true, &errmsg)))
         goto cleanup;
 
-    if (virCommandRun(livecmd, NULL) < 0)
+    if (virCommandRun(livecmd_update, NULL) < 0)
         goto cleanup;
 
     if (!(livecmd = nodeDeviceGetMdevctlModifyCommand(def, false, true, &errmsg)))