diff options
author | cl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk> | 2005-06-21 08:59:34 +0000 |
---|---|---|
committer | cl349@firebug.cl.cam.ac.uk <cl349@firebug.cl.cam.ac.uk> | 2005-06-21 08:59:34 +0000 |
commit | 1e6b0c78d874e263635cf4148932022ae55b8c92 (patch) | |
tree | 8914a214328e055cb40415715f73991de27293e8 /tools | |
parent | 26922e5f08c20a0cc95e51c6c7d7df78041638d3 (diff) | |
download | xen-1e6b0c78d874e263635cf4148932022ae55b8c92.tar.gz xen-1e6b0c78d874e263635cf4148932022ae55b8c92.tar.bz2 xen-1e6b0c78d874e263635cf4148932022ae55b8c92.zip |
bitkeeper revision 1.1724 (42b7d6f60v0z2ZEkw36W_1joFfySfw)
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@rustcorp.com.au>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/xenstore/xs.c | 8 | ||||
-rw-r--r-- | tools/xenstore/xs.h | 4 | ||||
-rw-r--r-- | tools/xenstore/xs_test.c | 4 |
3 files changed, 10 insertions, 6 deletions
diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c index e41ca652bd..c11e02ae1e 100644 --- a/tools/xenstore/xs.c +++ b/tools/xenstore/xs.c @@ -131,6 +131,7 @@ static int get_error(const char *errorstring) 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 @@ static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len) 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 @@ static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len) *type = msg.type; if (len) *len = msg.len; + ((char *)ret)[msg.len] = '\0'; return ret; } @@ -269,9 +271,9 @@ char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num) 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 --git a/tools/xenstore/xs.h b/tools/xenstore/xs.h index b778cedd65..09d5a20937 100644 --- a/tools/xenstore/xs.h +++ b/tools/xenstore/xs.h @@ -45,9 +45,9 @@ void xs_daemon_close(struct xs_handle *); */ 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 --git a/tools/xenstore/xs_test.c b/tools/xenstore/xs_test.c index 29929b7693..6ce5d701af 100644 --- a/tools/xenstore/xs_test.c +++ b/tools/xenstore/xs_test.c @@ -240,6 +240,8 @@ static void do_read(unsigned int handle, char *path) 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 @@ static void do_write(unsigned int handle, char *path, char *flags, char *data) 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); } |