WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-changelog

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

To: xen-changelog@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-changelog] Make xs_read/read_reply allocate an extra byte and put a nul character
From: BitKeeper Bot <riel@xxxxxxxxxxx>
Date: Tue, 21 Jun 2005 08:59:34 +0000
Cc: james@xxxxxxxxxxxxx
Delivery-date: Wed, 22 Jun 2005 15:00:52 +0000
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-changelog-request@lists.xensource.com?subject=help>
List-id: BK change log <xen-changelog.lists.xensource.com>
List-post: <mailto:xen-changelog@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/cgi-bin/mailman/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.xensource.com?subject=unsubscribe>
Reply-to: Xen Development List <xen-devel@xxxxxxxxxxxxxxxxxxx>
Sender: xen-changelog-bounces@xxxxxxxxxxxxxxxxxxx
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

<Prev in Thread] Current Thread [Next in Thread>
  • [Xen-changelog] Make xs_read/read_reply allocate an extra byte and put a nul character, BitKeeper Bot <=