ia64/xen-unstable

changeset 5522:2387d992079a

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>
author cl349@firebug.cl.cam.ac.uk
date Tue Jun 21 08:59:34 2005 +0000 (2005-06-21)
parents f2b4ab649a13
children 4cadd9fa93d5
files tools/xenstore/xs.c tools/xenstore/xs.h tools/xenstore/xs_test.c
line diff
     1.1 --- a/tools/xenstore/xs.c	Tue Jun 21 07:42:47 2005 +0000
     1.2 +++ b/tools/xenstore/xs.c	Tue Jun 21 08:59:34 2005 +0000
     1.3 @@ -131,6 +131,7 @@ static int get_error(const char *errorst
     1.4  	return xsd_errors[i].errnum;
     1.5  }
     1.6  
     1.7 +/* Adds extra nul terminator, because we generally (always?) hold strings. */
     1.8  static void *read_reply(int fd, enum xsd_sockmsg_type *type, unsigned int *len)
     1.9  {
    1.10  	struct xsd_sockmsg msg;
    1.11 @@ -140,7 +141,7 @@ static void *read_reply(int fd, enum xsd
    1.12  	if (!read_all(fd, &msg, sizeof(msg)))
    1.13  		return NULL;
    1.14  
    1.15 -	ret = malloc(msg.len);
    1.16 +	ret = malloc(msg.len + 1);
    1.17  	if (!ret)
    1.18  		return NULL;
    1.19  
    1.20 @@ -154,6 +155,7 @@ static void *read_reply(int fd, enum xsd
    1.21  	*type = msg.type;
    1.22  	if (len)
    1.23  		*len = msg.len;
    1.24 +	((char *)ret)[msg.len] = '\0';
    1.25  	return ret;
    1.26  }
    1.27  
    1.28 @@ -269,9 +271,9 @@ char **xs_directory(struct xs_handle *h,
    1.29  	return ret;
    1.30  }
    1.31  
    1.32 -/* Get the value of a single file.
    1.33 +/* Get the value of a single file, nul terminated.
    1.34   * Returns a malloced value: call free() on it after use.
    1.35 - * len indicates length in bytes.
    1.36 + * len indicates length in bytes, not including the nul.
    1.37   */
    1.38  void *xs_read(struct xs_handle *h, const char *path, unsigned int *len)
    1.39  {
     2.1 --- a/tools/xenstore/xs.h	Tue Jun 21 07:42:47 2005 +0000
     2.2 +++ b/tools/xenstore/xs.h	Tue Jun 21 08:59:34 2005 +0000
     2.3 @@ -45,9 +45,9 @@ void xs_daemon_close(struct xs_handle *)
     2.4   */
     2.5  char **xs_directory(struct xs_handle *h, const char *path, unsigned int *num);
     2.6  
     2.7 -/* Get the value of a single file.
     2.8 +/* Get the value of a single file, nul terminated.
     2.9   * Returns a malloced value: call free() on it after use.
    2.10 - * len indicates length in bytes.
    2.11 + * len indicates length in bytes, not including the nul.
    2.12   */
    2.13  void *xs_read(struct xs_handle *h, const char *path, unsigned int *len);
    2.14  
     3.1 --- a/tools/xenstore/xs_test.c	Tue Jun 21 07:42:47 2005 +0000
     3.2 +++ b/tools/xenstore/xs_test.c	Tue Jun 21 08:59:34 2005 +0000
     3.3 @@ -240,6 +240,8 @@ static void do_read(unsigned int handle,
     3.4  	if (!value)
     3.5  		failed(handle);
     3.6  
     3.7 +	/* It's supposed to nul terminate for us. */
     3.8 +	assert(value[len] == '\0');
     3.9  	if (handle)
    3.10  		printf("%i:%.*s\n", handle, len, value);
    3.11  	else
    3.12 @@ -261,7 +263,7 @@ static void do_write(unsigned int handle
    3.13  	else
    3.14  		barf("write flags 'none', 'create' or 'excl' only");
    3.15  
    3.16 -	if (!xs_write(handles[handle], path, data, strlen(data)+1, f))
    3.17 +	if (!xs_write(handles[handle], path, data, strlen(data), f))
    3.18  		failed(handle);
    3.19  }
    3.20