[Libguestfs] [PATCH] daemon: echo-daemon: do not crash on empty string

Richard W.M. Jones rjones at redhat.com
Tue Aug 19 11:17:42 UTC 2014


How about this instead?

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
-------------- next part --------------
>From dd333e5c04bc75f048104d108294fe7d9c98ccf3 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones at redhat.com>
Date: Tue, 19 Aug 2014 12:16:04 +0100
Subject: [PATCH] daemon: Fix echo-daemon so it doesn't fail on empty array
 input.

---
 daemon/echo-daemon.c | 45 +++++----------------------------------------
 generator/actions.ml |  4 +++-
 2 files changed, 8 insertions(+), 41 deletions(-)

diff --git a/daemon/echo-daemon.c b/daemon/echo-daemon.c
index c805819..d566a9b 100644
--- a/daemon/echo-daemon.c
+++ b/daemon/echo-daemon.c
@@ -26,48 +26,13 @@
 char *
 do_echo_daemon (char *const *argv)
 {
-  char *out = NULL;
-  size_t out_len = 0;
+  char *out;
 
-  /* Iterate over argv entries until reaching the NULL terminator */
-  while (*argv) {
-    char add_space = 0;
-
-    /* Store the end of current output */
-    size_t out_end = out_len;
-
-    /* Calculate the new output size */
-    size_t arg_len = strlen(*argv);
-    out_len += arg_len;
-
-    /* We will prepend a space if this isn't the first argument added */
-    if (NULL != out) {
-      out_len++;
-      add_space = 1;
-    }
-
-    /* Make the output buffer big enough for the string and its terminator */
-    char *out_new = realloc (out, out_len + 1);
-    if (NULL == out_new) {
-      reply_with_perror ("realloc");
-      free (out);
-      return 0;
-    }
-    out = out_new;
-
-    /* Prepend a space if required */
-    if (add_space) {
-      out[out_end++] = ' ';
-    }
-
-    /* Copy the argument to the output */
-    memcpy(&out[out_end], *argv, arg_len);
-
-    argv++;
+  out = join_strings (" ", argv);
+  if (out == NULL) {
+    reply_with_perror ("malloc");
+    return NULL;
   }
 
-  /* NULL terminate the output */
-  out[out_len] = '\0';
-
   return out;
 }
diff --git a/generator/actions.ml b/generator/actions.ml
index 9570d9b..bb95f7a 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -7210,7 +7210,9 @@ was built (see C<appliance/kmod.whitelist.in> in the source)." };
     proc_nr = Some 195;
     tests = [
       InitNone, Always, TestResultString (
-        [["echo_daemon"; "This is a test"]], "This is a test"), []
+        [["echo_daemon"; "This is a test"]], "This is a test"), [];
+      InitNone, Always, TestResultString (
+        [["echo_daemon"; ""]], ""), [];
     ];
     shortdesc = "echo arguments back to the client";
     longdesc = "\
-- 
2.0.4



More information about the Libguestfs mailing list