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

Peter Krempa pkrempa at redhat.com
Wed Feb 24 16:16:39 UTC 2021


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 at redhat.com>
---
 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




More information about the libvir-list mailing list