]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virCommandAddEnv: Make stealing of argument more obvious
authorPeter Krempa <pkrempa@redhat.com>
Tue, 23 Feb 2021 07:43:08 +0000 (08:43 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Mar 2021 08:50:19 +0000 (09:50 +0100)
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@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/util/vircommand.c

index 323f841b98192f0ac98e8d4187984cc39ae09713..b94ab615d58949247e50c06571e7d15dc48c38bc 100644 (file)
@@ -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);
 }
 
 /**