virCommandAddEnv: Make stealing of argument more obvious

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>
This commit is contained in:
Peter Krempa 2021-02-23 08:43:08 +01:00
parent b356a3ce7c
commit 13a9075cea

View 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);
}
/**