mirror of https://gitee.com/openkylin/libvirt.git
command: handle empty buffer argument correctly
virBufferContentAndReset (intentionally) returns NULL for a buffer with no content, but it is feasible to invoke a command with an explicit empty string. * src/util/command.c (virCommandAddEnvBuffer): Reject empty string. (virCommandAddArgBuffer): Allow explicit empty argument. * tests/commandtest.c (test9): Test it. * tests/commanddata/test9.log: Adjust.
This commit is contained in:
parent
c74a2a03f0
commit
2b045d39df
|
@ -983,6 +983,10 @@ virCommandAddEnvBuffer(virCommandPtr cmd, virBufferPtr buf)
|
|||
virBufferFreeAndReset(buf);
|
||||
return;
|
||||
}
|
||||
if (!virBufferUse(buf)) {
|
||||
cmd->has_error = EINVAL;
|
||||
return;
|
||||
}
|
||||
|
||||
cmd->env[cmd->nenv++] = virBufferContentAndReset(buf);
|
||||
}
|
||||
|
@ -1092,7 +1096,14 @@ virCommandAddArgBuffer(virCommandPtr cmd, virBufferPtr buf)
|
|||
return;
|
||||
}
|
||||
|
||||
cmd->args[cmd->nargs++] = virBufferContentAndReset(buf);
|
||||
cmd->args[cmd->nargs] = virBufferContentAndReset(buf);
|
||||
if (!cmd->args[cmd->nargs])
|
||||
cmd->args[cmd->nargs] = strdup("");
|
||||
if (!cmd->args[cmd->nargs]) {
|
||||
cmd->has_error = ENOMEM;
|
||||
return;
|
||||
}
|
||||
cmd->nargs++;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,8 +2,10 @@ ARG:-version
|
|||
ARG:-log=bar.log
|
||||
ARG:arg1
|
||||
ARG:arg2
|
||||
ARG:arg3
|
||||
ARG:
|
||||
ARG:arg4
|
||||
ARG:arg5
|
||||
ARG:arg6
|
||||
ENV:DISPLAY=:0.0
|
||||
ENV:HOME=/home/test
|
||||
ENV:HOSTNAME=test
|
||||
|
|
|
@ -352,11 +352,22 @@ static int test9(const void *unused ATTRIBUTE_UNUSED)
|
|||
{
|
||||
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
||||
const char* const args[] = { "arg1", "arg2", NULL };
|
||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||
|
||||
virCommandAddArg(cmd, "-version");
|
||||
virCommandAddArgPair(cmd, "-log", "bar.log");
|
||||
virCommandAddArgSet(cmd, args);
|
||||
virCommandAddArgList(cmd, "arg3", "arg4", NULL);
|
||||
virCommandAddArgBuffer(cmd, &buf);
|
||||
virBufferAddLit(&buf, "arg4");
|
||||
virCommandAddArgBuffer(cmd, &buf);
|
||||
virCommandAddArgList(cmd, "arg5", "arg6", NULL);
|
||||
|
||||
if (virBufferUse(&buf)) {
|
||||
printf("Buffer not transferred\n");
|
||||
virBufferFreeAndReset(&buf);
|
||||
virCommandFree(cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (virCommandRun(cmd, NULL) < 0) {
|
||||
virErrorPtr err = virGetLastError();
|
||||
|
|
Loading…
Reference in New Issue