]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virnetsocket: Don't free virCommand in virNetSocketNewConnectCommand()
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 15 Oct 2021 11:39:36 +0000 (13:39 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 15 Oct 2021 14:03:22 +0000 (16:03 +0200)
The aim of virNetSocketNewConnectCommand() is to execute passed
command and attach socket pair/pipe to it so that client socket
can be opened (this is used for connections with alternative
transports, e.g. ssh). The virCommand is created in a caller and
then passed to virNetSocketNewConnectCommand() where it is freed
using virCommandFree(). This approach is wrong on two levels:

1) The deallocation happens on a different level than allocation,
2) There's a WIN32 stub that just reports an error and doesn't
   free the command.

However, with g_autoptr() trickery the command can be freed in
caller.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/rpc/virnetsocket.c
tests/virnetsockettest.c

index 212089520de98727ab733e8b06d310799fd6cf7e..50c0c4ecc8da5be5fe3bed7a19be5e1bd62f161b 100644 (file)
@@ -821,8 +821,6 @@ int virNetSocketNewConnectCommand(virCommand *cmd,
     if (!(*retsock = virNetSocketNew(NULL, NULL, true, sv[0], errfd[0], pid, false)))
         goto error;
 
-    virCommandFree(cmd);
-
     return 0;
 
  error:
@@ -832,7 +830,6 @@ int virNetSocketNewConnectCommand(virCommand *cmd,
     VIR_FORCE_CLOSE(errfd[1]);
 
     virCommandAbort(cmd);
-    virCommandFree(cmd);
 
     return -1;
 }
@@ -856,7 +853,7 @@ int virNetSocketNewConnectSSH(const char *nodename,
                               const char *command,
                               virNetSocket **retsock)
 {
-    virCommand *cmd;
+    g_autoptr(virCommand) cmd = NULL;
 
     *retsock = NULL;
 
@@ -1154,7 +1151,7 @@ virNetSocketNewConnectLibssh(const char *host G_GNUC_UNUSED,
 int virNetSocketNewConnectExternal(const char **cmdargv,
                                    virNetSocket **retsock)
 {
-    virCommand *cmd;
+    g_autoptr(virCommand) cmd = NULL;
 
     *retsock = NULL;
 
index d24baa9a5ce83064f6c7f0d9038891a1b75ed3c9..33b8755ab7eeba2b2d9f3b94bf2255f2714fbdea 100644 (file)
@@ -388,7 +388,8 @@ static int testSocketCommandNormal(const void *data G_GNUC_UNUSED)
     char buf[100];
     size_t i;
     int ret = -1;
-    virCommand *cmd = virCommandNewArgList("/bin/cat", "/dev/zero", NULL);
+    g_autoptr(virCommand) cmd = virCommandNewArgList("/bin/cat", "/dev/zero", NULL);
+
     virCommandAddEnvPassCommon(cmd);
 
     if (virNetSocketNewConnectCommand(cmd, &csock) < 0)
@@ -415,7 +416,8 @@ static int testSocketCommandFail(const void *data G_GNUC_UNUSED)
     virNetSocket *csock = NULL; /* Client socket */
     char buf[100];
     int ret = -1;
-    virCommand *cmd = virCommandNewArgList("/bin/cat", "/dev/does-not-exist", NULL);
+    g_autoptr(virCommand) cmd = virCommandNewArgList("/bin/cat", "/dev/does-not-exist", NULL);
+
     virCommandAddEnvPassCommon(cmd);
 
     if (virNetSocketNewConnectCommand(cmd, &csock) < 0)