xenstore: sprintf->snprintf, and fix for OpenBSD.
authorKeir Fraser <keir@xensource.com>
Fri, 5 Oct 2007 13:11:36 +0000 (14:11 +0100)
committerKeir Fraser <keir@xensource.com>
Fri, 5 Oct 2007 13:11:36 +0000 (14:11 +0100)
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
tools/xenstore/xenstored_core.c
tools/xenstore/xenstored_transaction.c
tools/xenstore/xs.c
tools/xenstore/xs_lib.c
tools/xenstore/xs_lib.h
tools/xenstore/xsls.c

index 1b9b32d67d86b88a9d734e01d207bf26fde3361e..0ba5f05255b9543e1cf8e92cd03d1c31d4ffe48c 100644 (file)
@@ -685,7 +685,7 @@ static char *perms_to_strings(const void *ctx,
        char buffer[MAX_STRLEN(unsigned int) + 1];
 
        for (*len = 0, i = 0; i < num; i++) {
-               if (!xs_perm_to_string(&perms[i], buffer))
+               if (!xs_perm_to_string(&perms[i], buffer, sizeof(buffer)))
                        return NULL;
 
                strings = talloc_realloc(ctx, strings, char,
@@ -1659,7 +1659,7 @@ static void write_pidfile(const char *pidfile)
        if (lockf(fd, F_TLOCK, 0) == -1)
                exit(0);
 
-       len = sprintf(buf, "%ld\n", (long)getpid());
+       len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
        if (write(fd, buf, len) != len)
                barf_perror("Writing pid file %s", pidfile);
 }
index f079bdc7e15e19f1f99d85aed498c8cd84afcc39..380c691bb2db999746201a9607d75fdf862a5e0d 100644 (file)
@@ -181,7 +181,7 @@ void do_transaction_start(struct connection *conn, struct buffered_data *in)
        talloc_set_destructor(trans, destroy_transaction);
        conn->transaction_started++;
 
-       sprintf(id_str, "%u", trans->id);
+       snprintf(id_str, sizeof(id_str), "%u", trans->id);
        send_reply(conn, XS_TRANSACTION_START, id_str, strlen(id_str)+1);
 }
 
index ad1dc0860b70bd1134dbd7c68fe134768f04c9f6..faa7e5c80f406143fe6aee15e0129c091c8317bd 100644 (file)
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <sys/uio.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <string.h>
@@ -519,7 +520,7 @@ bool xs_set_permissions(struct xs_handle *h,
        for (i = 0; i < num_perms; i++) {
                char buffer[MAX_STRLEN(unsigned int)+1];
 
-               if (!xs_perm_to_string(&perms[i], buffer))
+               if (!xs_perm_to_string(&perms[i], buffer, sizeof(buffer)))
                        goto unwind;
 
                iov[i+1].iov_base = strdup(buffer);
@@ -687,9 +688,9 @@ bool xs_introduce_domain(struct xs_handle *h,
        char eventchn_str[MAX_STRLEN(eventchn)];
        struct iovec iov[3];
 
-       sprintf(domid_str, "%u", domid);
-       sprintf(mfn_str, "%lu", mfn);
-       sprintf(eventchn_str, "%u", eventchn);
+       snprintf(domid_str, sizeof(domid_str), "%u", domid);
+       snprintf(mfn_str, sizeof(mfn_str), "%lu", mfn);
+       snprintf(eventchn_str, sizeof(eventchn_str), "%u", eventchn);
 
        iov[0].iov_base = domid_str;
        iov[0].iov_len = strlen(domid_str) + 1;
@@ -708,7 +709,7 @@ static void * single_with_domid(struct xs_handle *h,
 {
        char domid_str[MAX_STRLEN(domid)];
 
-       sprintf(domid_str, "%u", domid);
+       snprintf(domid_str, sizeof(domid_str), "%u", domid);
 
        return xs_single(h, XBT_NULL, type, domid_str, NULL);
 }
@@ -728,7 +729,7 @@ char *xs_get_domain_path(struct xs_handle *h, unsigned int domid)
 {
        char domid_str[MAX_STRLEN(domid)];
 
-       sprintf(domid_str, "%u", domid);
+       snprintf(domid_str, sizeof(domid_str), "%u", domid);
 
        return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL);
 }
index 0afe87d325ab2c977b77627c3b1fa4509f279148..8375f15f0cdac052c7949d68f4cadc242435f90d 100644 (file)
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-#include "xs_lib.h"
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
+#include "xs_lib.h"
 
 /* Common routines for the Xen store daemon and client library. */
 
@@ -53,7 +53,7 @@ static const char *xs_daemon_path(void)
 const char *xs_daemon_tdb(void)
 {
        static char buf[PATH_MAX];
-       sprintf(buf, "%s/tdb", xs_daemon_rootdir());
+       snprintf(buf, sizeof(buf), "%s/tdb", xs_daemon_rootdir());
        return buf;
 }
 
@@ -143,7 +143,8 @@ bool xs_strings_to_perms(struct xs_permissions *perms, unsigned int num,
 }
 
 /* Convert permissions to a string (up to len MAX_STRLEN(unsigned int)+1). */
-bool xs_perm_to_string(const struct xs_permissions *perm, char *buffer)
+bool xs_perm_to_string(const struct xs_permissions *perm,
+                       char *buffer, size_t buf_len)
 {
        switch (perm->perms) {
        case XS_PERM_WRITE:
@@ -162,7 +163,7 @@ bool xs_perm_to_string(const struct xs_permissions *perm, char *buffer)
                errno = EINVAL;
                return false;
        }
-       sprintf(buffer+1, "%i", (int)perm->id);
+       snprintf(buffer+1, buf_len-1, "%i", (int)perm->id);
        return true;
 }
 
index f55008281add7f3f299871d3718132638c6ddd09..f56b7c3e2958a4e4a010967ccf52fb4320899286 100644 (file)
@@ -61,7 +61,8 @@ bool xs_strings_to_perms(struct xs_permissions *perms, unsigned int num,
                         const char *strings);
 
 /* Convert permissions to a string (up to len MAX_STRLEN(unsigned int)+1). */
-bool xs_perm_to_string(const struct xs_permissions *perm, char *buffer);
+bool xs_perm_to_string(const struct xs_permissions *perm,
+                       char *buffer, size_t buf_len);
 
 /* Given a string and a length, count how many strings (nul terms). */
 unsigned int xs_count_strings(const char *strings, unsigned int len);
index c9e41ef64e857b1a37e39a8ee822c7b41c96b950..71b76975bf3afe1655b96ebc2560ab9fe69d40df 100644 (file)
@@ -87,7 +87,7 @@ void print_dir(struct xs_handle *h, char *path, int cur_depth, int show_perms)
                 for (i = 0; i < nperms; i++) {
                     if (i)
                         putchar(',');
-                    xs_perm_to_string(perms+i, buf);
+                    xs_perm_to_string(perms+i, buf, sizeof(buf));
                     fputs(buf, stdout);
                 }
                 putchar(')');