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

[Xen-changelog] Make xs_read/read_reply allocate an extra byte and put a nul character



ChangeSet 1.1724, 2005/06/21 09:59:34+01:00, cl349@xxxxxxxxxxxxxxxxxxxx

        Make xs_read/read_reply allocate an extra byte and put a nul character
        at the end of objects to allow easy use as a string.
        From: Rusty Russell <rusty@xxxxxxxxxxxxxxx>                             
        
        Signed-off-by: Christian Limpach <Christian.Limpach@xxxxxxxxxxxx>



 xs.c      |    8 +++++---
 xs.h      |    4 ++--
 xs_test.c |    4 +++-
 3 files changed, 10 insertions(+), 6 deletions(-)


diff -Nru a/tools/xenstore/xs.c b/tools/xenstore/xs.c
--- a/tools/xenstore/xs.c       2005-06-22 11:02:00 -04:00
+++ b/tools/xenstore/xs.c       2005-06-22 11:02:00 -04:00
@@ -131,6 +131,7 @@
        return xsd_errors[i].errnum;
 }
 
+/* Adds extra nul terminator, because we generally (always?) hold strings. */
 static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
 {
        struct xsd_sockmsg msg;
@@ -140,7 +141,7 @@
        if (!read_all(fd, &msg, sizeof(msg)))
                return NULL;
 
-       ret = malloc(msg.len);
+       ret = malloc(msg.len + 1);
        if (!ret)
                return NULL;
 
@@ -154,6 +155,7 @@
        *type = msg.type;
        if (len)
                *len = msg.len;
+       ((char *)ret)[msg.len] = '\0';
        return ret;
 }
 
@@ -269,9 +271,9 @@
        return ret;
 }
 
-/* Get the value of a single file.
+/* Get the value of a single file, nul terminated.
  * Returns a malloced value: call free() on it after use.
- * len indicates length in bytes.
+ * len indicates length in bytes, not including the nul.
  */
 void *xs_read(struct xs_handle *h, const char *path, unsigned int *len)
 {
diff -Nru a/tools/xenstore/xs.h b/tools/xenstore/xs.h
--- a/tools/xenstore/xs.h       2005-06-22 11:02:00 -04:00
+++ b/tools/xenstore/xs.h       2005-06-22 11:02:00 -04:00
@@ -45,9 +45,9 @@
  */
 char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num);
 
-/* Get the value of a single file.
+/* Get the value of a single file, nul terminated.
  * Returns a malloced value: call free() on it after use.
- * len indicates length in bytes.
+ * len indicates length in bytes, not including the nul.
  */
 void *xs_read(struct xs_handle *h, const char *path, unsigned int *len);
 
diff -Nru a/tools/xenstore/xs_test.c b/tools/xenstore/xs_test.c
--- a/tools/xenstore/xs_test.c  2005-06-22 11:02:00 -04:00
+++ b/tools/xenstore/xs_test.c  2005-06-22 11:02:00 -04:00
@@ -240,6 +240,8 @@
        if (!value)
                failed(handle);
 
+       /* It's supposed to nul terminate for us. */
+       assert(value[len] == '\0');
        if (handle)
                printf("%i:%.*s\n", handle, len, value);
        else
@@ -261,7 +263,7 @@
        else
                barf("write flags 'none', 'create' or 'excl' only");
 
-       if (!xs_write(handles[handle], path, data, strlen(data)+1, f))
+       if (!xs_write(handles[handle], path, data, strlen(data), f))
                failed(handle);
 }
 

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.