[PATCH 04/33] virCommandAddEnv: Make stealing of argument more obvious

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The function is supposed to always consume the passed environment
variable string. Use a temp variable with autofree and g_steal_pointer
to prevent having to free it manually.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/vircommand.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 323f841b98..b94ab615d5 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -1325,8 +1325,10 @@ virCommandRawStatus(virCommandPtr cmd)
  * already set, then it is replaced in the list.
  */
 static void
-virCommandAddEnv(virCommandPtr cmd, char *env)
+virCommandAddEnv(virCommandPtr cmd,
+                 char *envstr)
 {
+    g_autofree char *env = envstr;
     size_t namelen;
     size_t i;

@@ -1336,19 +1338,18 @@ virCommandAddEnv(virCommandPtr cmd, char *env)
         /* + 1 because we want to match the '=' character too. */
         if (STREQLEN(cmd->env[i], env, namelen + 1)) {
             VIR_FREE(cmd->env[i]);
-            cmd->env[i] = env;
+            cmd->env[i] = g_steal_pointer(&env);
             return;
         }
     }

     /* Arg plus trailing NULL. */
     if (VIR_RESIZE_N(cmd->env, cmd->maxenv, cmd->nenv, 1 + 1) < 0) {
-        VIR_FREE(env);
         cmd->has_error = ENOMEM;
         return;
     }

-    cmd->env[cmd->nenv++] = env;
+    cmd->env[cmd->nenv++] = g_steal_pointer(&env);
 }

 /**
-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux