[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 1/2] 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.
---
 src/util/command.c          |   13 ++++++++++++-
 tests/commanddata/test9.log |    4 +++-
 tests/commandtest.c         |   13 ++++++++++++-
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/util/command.c b/src/util/command.c
index c3ce361..f5effdf 100644
--- a/src/util/command.c
+++ b/src/util/command.c
@@ -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++;
 }


diff --git a/tests/commanddata/test9.log b/tests/commanddata/test9.log
index 2607530..3a93c19 100644
--- a/tests/commanddata/test9.log
+++ b/tests/commanddata/test9.log
@@ -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
diff --git a/tests/commandtest.c b/tests/commandtest.c
index dd6c248..efc48fe 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -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();
-- 
1.7.7.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]