提交 2b045d39 编写于 作者: E Eric Blake

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.
上级 c74a2a03
...@@ -983,6 +983,10 @@ virCommandAddEnvBuffer(virCommandPtr cmd, virBufferPtr buf) ...@@ -983,6 +983,10 @@ virCommandAddEnvBuffer(virCommandPtr cmd, virBufferPtr buf)
virBufferFreeAndReset(buf); virBufferFreeAndReset(buf);
return; return;
} }
if (!virBufferUse(buf)) {
cmd->has_error = EINVAL;
return;
}
cmd->env[cmd->nenv++] = virBufferContentAndReset(buf); cmd->env[cmd->nenv++] = virBufferContentAndReset(buf);
} }
...@@ -1092,7 +1096,14 @@ virCommandAddArgBuffer(virCommandPtr cmd, virBufferPtr buf) ...@@ -1092,7 +1096,14 @@ virCommandAddArgBuffer(virCommandPtr cmd, virBufferPtr buf)
return; 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 ...@@ -2,8 +2,10 @@ ARG:-version
ARG:-log=bar.log ARG:-log=bar.log
ARG:arg1 ARG:arg1
ARG:arg2 ARG:arg2
ARG:arg3 ARG:
ARG:arg4 ARG:arg4
ARG:arg5
ARG:arg6
ENV:DISPLAY=:0.0 ENV:DISPLAY=:0.0
ENV:HOME=/home/test ENV:HOME=/home/test
ENV:HOSTNAME=test ENV:HOSTNAME=test
......
...@@ -352,11 +352,22 @@ static int test9(const void *unused ATTRIBUTE_UNUSED) ...@@ -352,11 +352,22 @@ static int test9(const void *unused ATTRIBUTE_UNUSED)
{ {
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper"); virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
const char* const args[] = { "arg1", "arg2", NULL }; const char* const args[] = { "arg1", "arg2", NULL };
virBuffer buf = VIR_BUFFER_INITIALIZER;
virCommandAddArg(cmd, "-version"); virCommandAddArg(cmd, "-version");
virCommandAddArgPair(cmd, "-log", "bar.log"); virCommandAddArgPair(cmd, "-log", "bar.log");
virCommandAddArgSet(cmd, args); 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) { if (virCommandRun(cmd, NULL) < 0) {
virErrorPtr err = virGetLastError(); virErrorPtr err = virGetLastError();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册