Richard W.M. Jones
2012-Mar-13 08:26 UTC
[Libguestfs] [PATCH 0/2] 'int' to 'size_t' changes
These two patches are probably not completely independent, but separating them is a lot of work. With *both* patches applied, all the tests and extra-tests pass. That's no guarantee however that there isn't a mistake, so I don't think this patch is a candidate for the 1.16 branch, until it's had a lot more testing in development. Rich.
Richard W.M. Jones
2012-Mar-13 08:26 UTC
[Libguestfs] [PATCH 1/2] daemon: Implement a growable strings buffer type.
From: "Richard W.M. Jones" <rjones at redhat.com> Previously a lot of daemon code used three variables (a string list, 'int size' and 'int alloc') to track growable strings buffers. This commit implements a simple struct containing the same variables, but using size_t instead of int: struct stringsbuf { char **argv; size_t size; size_t alloc; }; Use it like this: DECLARE_STRINGSBUF (ret); //... if (add_string (&ret, str) == -1) return NULL; //... if (end_stringsbuf (&ret) == -1) return NULL; return ret.argv; --- daemon/9p.c | 23 +++++++++++------------ daemon/available.c | 9 ++++----- daemon/blkid.c | 38 ++++++++++++++++++-------------------- daemon/daemon.h | 33 +++++++++++++++++++++++++++------ daemon/devsparts.c | 50 +++++++++++++++++++++++--------------------------- daemon/ext2.c | 17 ++++++++--------- daemon/file.c | 11 +++++------ daemon/find.c | 18 +++++++++--------- daemon/guestfsd.c | 51 +++++++++++++++++++++++++++------------------------ daemon/initrd.c | 11 +++++------ daemon/inotify.c | 11 +++++------ daemon/link.c | 9 ++++----- daemon/ls.c | 15 ++++++++------- daemon/lvm.c | 32 ++++++++++++++++---------------- daemon/md.c | 29 +++++++++++++++-------------- daemon/mount.c | 48 ++++++++++++++++++++++++------------------------ 16 files changed, 209 insertions(+), 196 deletions(-) diff --git a/daemon/9p.c b/daemon/9p.c index 8476c45..6243919 100644 --- a/daemon/9p.c +++ b/daemon/9p.c @@ -40,8 +40,7 @@ static char *read_whole_file (const char *filename); char ** do_list_9p (void) { - char **r = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (r); DIR *dir; @@ -55,10 +54,10 @@ do_list_9p (void) * the virtio driver isn't loaded. Don't return an error * in this case, but return an empty list. */ - if (add_string (&r, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&r) == -1) return NULL; - return r; + return r.argv; } while (1) { @@ -79,7 +78,7 @@ do_list_9p (void) if (mount_tag == 0) continue; - if (add_string (&r, &size, &alloc, mount_tag) == -1) { + if (add_string (&r, mount_tag) == -1) { free (mount_tag); closedir (dir); return NULL; @@ -92,7 +91,7 @@ do_list_9p (void) /* Check readdir didn't fail */ if (errno != 0) { reply_with_perror ("readdir: /sys/block"); - free_stringslen (r, size); + free_stringslen (r.argv, r.size); closedir (dir); return NULL; } @@ -100,19 +99,19 @@ do_list_9p (void) /* Close the directory handle */ if (closedir (dir) == -1) { reply_with_perror ("closedir: /sys/block"); - free_stringslen (r, size); + free_stringslen (r.argv, r.size); return NULL; } - /* Sort the tags. Note that r might be NULL if there are no tags. */ - if (r != NULL) - sort_strings (r, size); + /* Sort the tags. */ + if (r.size > 0) + sort_strings (r.argv, r.size); /* NULL terminate the list */ - if (add_string (&r, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&r) == -1) return NULL; - return r; + return r.argv; } /* Read whole file into dynamically allocated array. If there is an diff --git a/daemon/available.c b/daemon/available.c index 6d2aea2..97f1558 100644 --- a/daemon/available.c +++ b/daemon/available.c @@ -58,16 +58,15 @@ char ** do_available_all_groups (void) { size_t i; - char **groups = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (groups); for (i = 0; optgroups[i].group != NULL; ++i) { - if (add_string (&groups, &size, &alloc, optgroups[i].group) == -1) + if (add_string (&groups, optgroups[i].group) == -1) return NULL; } - if (add_string (&groups, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&groups) == -1) return NULL; - return groups; /* caller frees */ + return groups.argv; /* caller frees */ } diff --git a/daemon/blkid.c b/daemon/blkid.c index 83cf7f5..a6170dd 100644 --- a/daemon/blkid.c +++ b/daemon/blkid.c @@ -132,8 +132,7 @@ blkid_with_p_i_opt (const char *device) int r; char *out = NULL, *err = NULL; char **lines = NULL; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); r = command (&out, &err, "blkid", "-c", "/dev/null", "-p", "-i", "-o", "export", device, NULL); @@ -176,28 +175,28 @@ blkid_with_p_i_opt (const char *device) *eq = '\0'; eq++; /* Add the key/value pair to the output */ - if (add_string (&ret, &size, &alloc, line) == -1 || - add_string (&ret, &size, &alloc, eq) == -1) goto error; + if (add_string (&ret, line) == -1 || + add_string (&ret, eq) == -1) goto error; } else { fprintf (stderr, "blkid: unexpected blkid output ignored: %s", line); } } - if (add_string (&ret, &size, &alloc, NULL) == -1) goto error; + if (end_stringsbuf (&ret) == -1) goto error; free (out); free (err); free_strings (lines); - return ret; + return ret.argv; error: free (out); free (err); if (lines) free_strings (lines); - if (ret) - free_strings (ret); + if (ret.argv) + free_strings (ret.argv); return NULL; } @@ -206,33 +205,32 @@ static char ** blkid_without_p_i_opt(const char *device) { char *s; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); - if (add_string (&ret, &size, &alloc, "TYPE") == -1) goto error; + if (add_string (&ret, "TYPE") == -1) goto error; s = get_blkid_tag (device, "TYPE"); if (s == NULL) goto error; - if (add_string (&ret, &size, &alloc, s) == -1) + if (add_string (&ret, s) == -1) goto error; - if (add_string (&ret, &size, &alloc, "LABEL") == -1) goto error; + if (add_string (&ret, "LABEL") == -1) goto error; s = get_blkid_tag (device, "LABEL"); if (s == NULL) goto error; - if (add_string (&ret, &size, &alloc, s) == -1) + if (add_string (&ret, s) == -1) goto error; - if (add_string (&ret, &size, &alloc, "UUID") == -1) goto error; + if (add_string (&ret, "UUID") == -1) goto error; s = get_blkid_tag (device, "UUID"); if (s == NULL) goto error; - if (add_string (&ret, &size, &alloc, s) == -1) + if (add_string (&ret, s) == -1) goto error; - if (add_string (&ret, &size, &alloc, NULL) == -1) goto error; + if (end_stringsbuf (&ret) == -1) goto error; - return ret; + return ret.argv; error: - if (ret) - free_strings (ret); + if (ret.argv) + free_strings (ret.argv); return NULL; } diff --git a/daemon/daemon.h b/daemon/daemon.h index b515fe4..b973a7f 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -47,14 +47,35 @@ extern int xwrite (int sock, const void *buf, size_t len) extern int xread (int sock, void *buf, size_t len) __attribute__((__warn_unused_result__)); -extern int add_string_nodup (char ***argv, int *size, int *alloc, char *str); -extern int add_string (char ***argv, int *size, int *alloc, const char *str); +/* Growable strings buffer. */ +struct stringsbuf { + char **argv; + size_t size; + size_t alloc; +}; +#define DECLARE_STRINGSBUF(v) \ + struct stringsbuf (v) = { .argv = NULL, .size = 0, .alloc = 0 } + +/* Append a string to the strings buffer. + * + * add_string_nodup: don't copy the string. + * add_string: copy the string. + * end_stringsbuf: NULL-terminate the buffer. + * + * All functions may fail. If these functions return -1, then + * reply_with_* has been called, the strings have been freed and the + * buffer should no longer be used. + */ +extern int add_string_nodup (struct stringsbuf *sb, char *str); +extern int add_string (struct stringsbuf *sb, const char *str); +extern int end_stringsbuf (struct stringsbuf *sb); + extern size_t count_strings (char *const *argv); -extern void sort_strings (char **argv, int len); +extern void sort_strings (char **argv, size_t len); extern void free_strings (char **argv); -extern void free_stringslen (char **argv, int len); +extern void free_stringslen (char **argv, size_t len); -extern int is_power_of_2 (unsigned long v); +extern char **split_lines (char *str); #define command(out,err,name,...) commandf((out),(err),0,(name),__VA_ARGS__) #define commandr(out,err,name,...) commandrf((out),(err),0,(name),__VA_ARGS__) @@ -74,7 +95,7 @@ extern int commandvf (char **stdoutput, char **stderror, int flags, extern int commandrvf (char **stdoutput, char **stderror, int flags, char const* const *argv); -extern char **split_lines (char *str); +extern int is_power_of_2 (unsigned long v); extern void trim (char *str); diff --git a/daemon/devsparts.c b/daemon/devsparts.c index 440739f..6fb0097 100644 --- a/daemon/devsparts.c +++ b/daemon/devsparts.c @@ -31,15 +31,13 @@ #include "daemon.h" #include "actions.h" -typedef int (*block_dev_func_t)(const char *dev, - char ***r, int *size, int *alloc); +typedef int (*block_dev_func_t) (const char *dev, struct stringsbuf *r); /* Execute a given function for each discovered block device */ -static char** +static char ** foreach_block_device (block_dev_func_t func) { - char **r = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (r); DIR *dir; int err = 0; @@ -78,7 +76,7 @@ foreach_block_device (block_dev_func_t func) close (fd); /* Call the map function for this device */ - if((*func)(d->d_name, &r, &size, &alloc) != 0) { + if((*func)(d->d_name, &r) != 0) { err = 1; break; } @@ -88,7 +86,7 @@ foreach_block_device (block_dev_func_t func) /* Check readdir didn't fail */ if(0 != errno) { reply_with_perror ("readdir: /sys/block"); - free_stringslen(r, size); + free_stringslen (r.argv, r.size); closedir (dir); return NULL; } @@ -96,37 +94,36 @@ foreach_block_device (block_dev_func_t func) /* Close the directory handle */ if (closedir (dir) == -1) { reply_with_perror ("closedir: /sys/block"); - free_stringslen(r, size); + free_stringslen (r.argv, r.size); return NULL; } /* Free the result list on error */ - if(err) { - free_stringslen(r, size); + if (err) { + free_stringslen (r.argv, r.size); return NULL; } - /* Sort the devices. Note that r might be NULL if there are no devices. */ - if (r != NULL) - sort_strings (r, size); + /* Sort the devices. */ + if (r.size > 0) + sort_strings (r.argv, r.size); /* NULL terminate the list */ - if (add_string (&r, &size, &alloc, NULL) == -1) { + if (end_stringsbuf (&r) == -1) { return NULL; } - return r; + return r.argv; } /* Add a device to the list of devices */ static int -add_device(const char *device, - char ***const r, int *const size, int *const alloc) +add_device (const char *device, struct stringsbuf *r) { char dev_path[256]; snprintf (dev_path, sizeof dev_path, "/dev/%s", device); - if (add_string (r, size, alloc, dev_path) == -1) { + if (add_string (r, dev_path) == -1) { return -1; } @@ -136,12 +133,11 @@ add_device(const char *device, char ** do_list_devices (void) { - return foreach_block_device(add_device); + return foreach_block_device (add_device); } static int -add_partitions(const char *device, - char ***const r, int *const size, int *const alloc) +add_partitions (const char *device, struct stringsbuf *r) { char devdir[256]; @@ -151,7 +147,7 @@ add_partitions(const char *device, DIR *dir = opendir (devdir); if (!dir) { reply_with_perror ("opendir: %s", devdir); - free_stringslen (*r, *size); + free_stringslen (r->argv, r->size); return -1; } @@ -165,7 +161,7 @@ add_partitions(const char *device, char part[256]; snprintf (part, sizeof part, "/dev/%s", d->d_name); - if (add_string (r, size, alloc, part) == -1) { + if (add_string (r, part) == -1) { closedir (dir); return -1; } @@ -173,9 +169,9 @@ add_partitions(const char *device, } /* Check if readdir failed */ - if(0 != errno) { + if (0 != errno) { reply_with_perror ("readdir: %s", devdir); - free_stringslen(*r, *size); + free_stringslen (r->argv, r->size); closedir (dir); return -1; } @@ -183,7 +179,7 @@ add_partitions(const char *device, /* Close the directory handle */ if (closedir (dir) == -1) { reply_with_perror ("closedir: /sys/block/%s", device); - free_stringslen (*r, *size); + free_stringslen (r->argv, r->size); return -1; } @@ -193,7 +189,7 @@ add_partitions(const char *device, char ** do_list_partitions (void) { - return foreach_block_device(add_partitions); + return foreach_block_device (add_partitions); } char * diff --git a/daemon/ext2.c b/daemon/ext2.c index 09037fb..5defbc8 100644 --- a/daemon/ext2.c +++ b/daemon/ext2.c @@ -63,8 +63,7 @@ do_tune2fs_l (const char *device) int r; char *out, *err; char *p, *pend, *colon; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); char prog[] = "tune2fs"; if (e2prog (prog) == -1) @@ -108,30 +107,30 @@ do_tune2fs_l (const char *device) do { colon++; } while (*colon && c_isspace (*colon)); - if (add_string (&ret, &size, &alloc, p) == -1) { + if (add_string (&ret, p) == -1) { free (out); return NULL; } if (STREQ (colon, "<none>") || STREQ (colon, "<not available>") || STREQ (colon, "(none)")) { - if (add_string (&ret, &size, &alloc, "") == -1) { + if (add_string (&ret, "") == -1) { free (out); return NULL; } } else { - if (add_string (&ret, &size, &alloc, colon) == -1) { + if (add_string (&ret, colon) == -1) { free (out); return NULL; } } } else { - if (add_string (&ret, &size, &alloc, p) == -1) { + if (add_string (&ret, p) == -1) { free (out); return NULL; } - if (add_string (&ret, &size, &alloc, "") == -1) { + if (add_string (&ret, "") == -1) { free (out); return NULL; } @@ -142,10 +141,10 @@ do_tune2fs_l (const char *device) free (out); - if (add_string (&ret, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&ret) == -1) return NULL; - return ret; + return ret.argv; } int diff --git a/daemon/file.c b/daemon/file.c index 5235446..161ea31 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -156,8 +156,7 @@ do_cat (const char *path) char ** do_read_lines (const char *path) { - char **r = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (r); FILE *fp; char *line = NULL; size_t len = 0; @@ -179,7 +178,7 @@ do_read_lines (const char *path) else if (n >= 1 && line[n-1] == '\n') line[n-1] = '\0'; - if (add_string (&r, &size, &alloc, line) == -1) { + if (add_string (&r, line) == -1) { free (line); fclose (fp); return NULL; @@ -188,18 +187,18 @@ do_read_lines (const char *path) free (line); - if (add_string (&r, &size, &alloc, NULL) == -1) { + if (end_stringsbuf (&r) == -1) { fclose (fp); return NULL; } if (fclose (fp) == EOF) { reply_with_perror ("fclose: %s", path); - free_strings (r); + free_stringslen (r.argv, r.size); return NULL; } - return r; + return r.argv; } int diff --git a/daemon/find.c b/daemon/find.c index 0b8c243..014712e 100644 --- a/daemon/find.c +++ b/daemon/find.c @@ -55,8 +55,7 @@ do_find (const char *dir) int r, len, sysrootdirlen; char *cmd; FILE *fp; - char **res = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); char *sysrootdir; char str[PATH_MAX]; @@ -105,28 +104,29 @@ do_find (const char *dir) continue; /* Remove the directory part of the path when adding it. */ - if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) { + if (add_string (&ret, str + sysrootdirlen) == -1) { pclose (fp); return NULL; } } if (pclose (fp) != 0) { reply_with_perror ("pclose"); - free_stringslen (res, size); + free_stringslen (ret.argv, ret.size); return NULL; } if (r == -1) { - free_stringslen (res, size); + free_stringslen (ret.argv, ret.size); return NULL; } - if (add_string (&res, &size, &alloc, NULL) == -1) - return NULL; + if (ret.size > 0) + sort_strings (ret.argv, ret.size); - sort_strings (res, size-1); + if (end_stringsbuf (&ret) == -1) + return NULL; - return res; /* caller frees */ + return ret.argv; /* caller frees */ } /* The code below assumes each path returned can fit into a protocol diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 721c169..6174074 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -417,46 +417,50 @@ xread (int sock, void *v_buf, size_t len) } int -add_string_nodup (char ***argv, int *size, int *alloc, char *str) +add_string_nodup (struct stringsbuf *sb, char *str) { char **new_argv; - if (*size >= *alloc) { - *alloc += 64; - new_argv = realloc (*argv, *alloc * sizeof (char *)); + if (sb->size >= sb->alloc) { + sb->alloc += 64; + new_argv = realloc (sb->argv, sb->alloc * sizeof (char *)); if (new_argv == NULL) { reply_with_perror ("realloc"); - free_strings (*argv); - *argv = NULL; + free_stringslen (sb->argv, sb->size); + sb->argv = NULL; return -1; } - *argv = new_argv; + sb->argv = new_argv; } - (*argv)[*size] = str; + sb->argv[sb->size] = str; + sb->size++; - (*size)++; return 0; } int -add_string (char ***argv, int *size, int *alloc, const char *str) +add_string (struct stringsbuf *sb, const char *str) { - char *new_str; + char *new_str = NULL; if (str) { new_str = strdup (str); if (new_str == NULL) { reply_with_perror ("strdup"); - free_strings (*argv); - *argv = NULL; + free_stringslen (sb->argv, sb->size); + sb->argv = NULL; return -1; } - } else { - new_str = NULL; } - return add_string_nodup (argv, size, alloc, new_str); + return add_string_nodup (sb, new_str); +} + +int +end_stringsbuf (struct stringsbuf *sb) +{ + return add_string_nodup (sb, NULL); } size_t @@ -485,7 +489,7 @@ compare (const void *vp1, const void *vp2) } void -sort_strings (char **argv, int len) +sort_strings (char **argv, size_t len) { qsort (argv, len, sizeof (char *), compare); } @@ -501,9 +505,9 @@ free_strings (char **argv) } void -free_stringslen (char **argv, int len) +free_stringslen (char **argv, size_t len) { - int i; + size_t i; for (i = 0; i < len; ++i) free (argv[i]); @@ -946,8 +950,7 @@ commandrvf (char **stdoutput, char **stderror, int flags, char ** split_lines (char *str) { - char **lines = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (lines); char *p, *pend; if (STREQ (str, "")) @@ -965,7 +968,7 @@ split_lines (char *str) pend++; } - if (add_string (&lines, &size, &alloc, p) == -1) { + if (add_string (&lines, p) == -1) { return NULL; } @@ -973,10 +976,10 @@ split_lines (char *str) } empty_list: - if (add_string (&lines, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&lines) == -1) return NULL; - return lines; + return lines.argv; } /* Skip leading and trailing whitespace, updating the original string diff --git a/daemon/initrd.c b/daemon/initrd.c index d9c7050..c1ef2d1 100644 --- a/daemon/initrd.c +++ b/daemon/initrd.c @@ -36,8 +36,7 @@ do_initrd_list (const char *path) FILE *fp; char *cmd; char filename[PATH_MAX]; - char **filenames = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (filenames); size_t len; /* "zcat /sysroot/<path> | cpio --quiet -it", but path must be quoted. */ @@ -62,24 +61,24 @@ do_initrd_list (const char *path) if (len > 0 && filename[len-1] == '\n') filename[len-1] = '\0'; - if (add_string (&filenames, &size, &alloc, filename) == -1) { + if (add_string (&filenames, filename) == -1) { pclose (fp); return NULL; } } - if (add_string (&filenames, &size, &alloc, NULL) == -1) { + if (end_stringsbuf (&filenames) == -1) { pclose (fp); return NULL; } if (pclose (fp) != 0) { reply_with_perror ("pclose"); - free_strings (filenames); + free_stringslen (filenames.argv, filenames.size); return NULL; } - return filenames; + return filenames.argv; } char * diff --git a/daemon/inotify.c b/daemon/inotify.c index 6c00fd0..ed425b8 100644 --- a/daemon/inotify.c +++ b/daemon/inotify.c @@ -301,8 +301,7 @@ do_inotify_read (void) char ** do_inotify_files (void) { - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); unsigned int i; FILE *fp = NULL; guestfs_int_inotify_event_list *events; @@ -359,23 +358,23 @@ do_inotify_files (void) } while (fgets (buf, sizeof buf, fp) != NULL) { - int len = strlen (buf); + size_t len = strlen (buf); if (len > 0 && buf[len-1] == '\n') buf[len-1] = '\0'; - if (add_string (&ret, &size, &alloc, buf) == -1) + if (add_string (&ret, buf) == -1) goto error; } fclose (fp); /* implicitly closes fd */ fp = NULL; - if (add_string (&ret, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&ret) == -1) goto error; unlink (tempfile); - return ret; + return ret.argv; error: if (fp != NULL) diff --git a/daemon/link.c b/daemon/link.c index b27e72c..a8162a2 100644 --- a/daemon/link.c +++ b/daemon/link.c @@ -60,8 +60,7 @@ do_readlinklist (const char *path, char *const *names) ssize_t r; char link[PATH_MAX]; const char *str; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); CHROOT_IN; fd_cwd = open (path, O_RDONLY | O_DIRECTORY); @@ -87,7 +86,7 @@ do_readlinklist (const char *path, char *const *names) str = link; } else str = ""; - if (add_string (&ret, &size, &alloc, str) == -1) { + if (add_string (&ret, str) == -1) { close (fd_cwd); return NULL; } @@ -95,10 +94,10 @@ do_readlinklist (const char *path, char *const *names) close (fd_cwd); - if (add_string (&ret, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&ret) == -1) return NULL; - return ret; + return ret.argv; } static int diff --git a/daemon/ls.c b/daemon/ls.c index 1df0b6a..5c76d7e 100644 --- a/daemon/ls.c +++ b/daemon/ls.c @@ -32,8 +32,7 @@ char ** do_ls (const char *path) { - char **r = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); DIR *dir; struct dirent *d; @@ -50,25 +49,27 @@ do_ls (const char *path) if (STREQ (d->d_name, ".") || STREQ (d->d_name, "..")) continue; - if (add_string (&r, &size, &alloc, d->d_name) == -1) { + if (add_string (&ret, d->d_name) == -1) { closedir (dir); return NULL; } } - if (add_string (&r, &size, &alloc, NULL) == -1) { + if (ret.size > 0) + sort_strings (ret.argv, ret.size); + + if (end_stringsbuf (&ret) == -1) { closedir (dir); return NULL; } if (closedir (dir) == -1) { reply_with_perror ("closedir: %s", path); - free_strings (r); + free_stringslen (ret.argv, ret.size); return NULL; } - sort_strings (r, size-1); - return r; + return ret.argv; } /* Because we can't chroot and run the ls command (since 'ls' won't diff --git a/daemon/lvm.c b/daemon/lvm.c index 9a71c65..8b56921 100644 --- a/daemon/lvm.c +++ b/daemon/lvm.c @@ -45,9 +45,8 @@ static char ** convert_lvm_output (char *out, const char *prefix) { char *p, *pend; - char **r = NULL; - int size = 0, alloc = 0; int len; + DECLARE_STRINGSBUF (ret); char buf[256]; char *str; @@ -79,7 +78,7 @@ convert_lvm_output (char *out, const char *prefix) } else str = p; - if (add_string (&r, &size, &alloc, str) == -1) { + if (add_string (&ret, str) == -1) { free (out); return NULL; } @@ -89,11 +88,13 @@ convert_lvm_output (char *out, const char *prefix) free (out); - if (add_string (&r, &size, &alloc, NULL) == -1) + if (ret.size > 0) + sort_strings (ret.argv, ret.size); + + if (end_stringsbuf (&ret) == -1) return NULL; - sort_strings (r, size-1); - return r; + return ret.argv; } char ** @@ -769,8 +770,7 @@ do_lvm_canonical_lv_name (const char *device) char ** do_list_dm_devices (void) { - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); struct dirent *d; DIR *dir; int r; @@ -802,7 +802,7 @@ do_list_dm_devices (void) /* Ignore dm devices which are LVs. */ r = lv_canonical (devname, NULL); if (r == -1) { - free_stringslen (ret, size); + free_stringslen (ret.argv, ret.size); closedir (dir); return NULL; } @@ -810,7 +810,7 @@ do_list_dm_devices (void) continue; /* Not an LV, so add it. */ - if (add_string (&ret, &size, &alloc, devname) == -1) { + if (add_string (&ret, devname) == -1) { closedir (dir); return NULL; } @@ -819,7 +819,7 @@ do_list_dm_devices (void) /* Did readdir fail? */ if (errno != 0) { reply_with_perror ("readdir: /dev/mapper"); - free_stringslen (ret, size); + free_stringslen (ret.argv, ret.size); closedir (dir); return NULL; } @@ -827,17 +827,17 @@ do_list_dm_devices (void) /* Close the directory handle. */ if (closedir (dir) == -1) { reply_with_perror ("closedir: /dev/mapper"); - free_stringslen (ret, size); + free_stringslen (ret.argv, ret.size); return NULL; } /* Sort the output (may be empty). */ - if (ret != NULL) - sort_strings (ret, size); + if (ret.size > 0) + sort_strings (ret.argv, ret.size); /* NULL-terminate the list. */ - if (add_string (&ret, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&ret) == -1) return NULL; - return ret; + return ret.argv; } diff --git a/daemon/md.c b/daemon/md.c index 41e2c75..78e84f3 100644 --- a/daemon/md.c +++ b/daemon/md.c @@ -180,8 +180,7 @@ glob_errfunc (const char *epath, int eerrno) char ** do_list_md_devices (void) { - char **r = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); glob_t mds; memset(&mds, 0, sizeof(mds)); @@ -218,17 +217,19 @@ do_list_md_devices (void) n = mempcpy(n, &mds.gl_pathv[i][strlen(PREFIX)], len); *n = '\0'; - if (add_string_nodup (&r, &size, &alloc, dev) == -1) goto error; + if (add_string_nodup (&ret, dev) == -1) goto error; } - if (add_string_nodup (&r, &size, &alloc, NULL) == -1) goto error; + if (end_stringsbuf (&ret) == -1) goto error; globfree (&mds); - return r; + return ret.argv; error: globfree (&mds); - if (r != NULL) free_strings (r); + if (ret.argv != NULL) + free_stringslen (ret.argv, ret.size); + return NULL; } @@ -241,8 +242,7 @@ do_md_detail(const char *md) char *out = NULL, *err = NULL; char **lines = NULL; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); const char *mdadm[] = { "mdadm", "-D", "--export", md, NULL }; r = commandv (&out, &err, mdadm); @@ -286,8 +286,8 @@ do_md_detail(const char *md) } /* Add the key/value pair to the output */ - if (add_string (&ret, &size, &alloc, line) == -1 || - add_string (&ret, &size, &alloc, eq) == -1) goto error; + if (add_string (&ret, line) == -1 || + add_string (&ret, eq) == -1) goto error; } else { /* Ignore lines with no equals sign (shouldn't happen). Log to stderr so * it will show up in LIBGUESTFS_DEBUG. */ @@ -299,17 +299,18 @@ do_md_detail(const char *md) free (err); free_strings (lines); - if (add_string (&ret, &size, &alloc, NULL) == -1) return NULL; + if (end_stringsbuf (&ret) == -1) + return NULL; - return ret; + return ret.argv; error: free (out); free (err); if (lines) free_strings (lines); - if (ret) - free_strings (ret); + if (ret.argv != NULL) + free_stringslen (ret.argv, ret.size); return NULL; } diff --git a/daemon/mount.c b/daemon/mount.c index 0c393f7..5e74ce8 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -224,8 +224,7 @@ mounts_or_mountpoints (int mp) { FILE *fp; struct mntent *m; - char **ret = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (ret); size_t i; int r; @@ -241,42 +240,43 @@ mounts_or_mountpoints (int mp) while ((m = getmntent (fp)) != NULL) { /* Allow a mount directory like "/sysroot". */ if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) { - if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) { + if (add_string (&ret, m->mnt_fsname) == -1) { error: endmntent (fp); return NULL; } if (mp && - add_string (&ret, &size, &alloc, "/") == -1) + add_string (&ret, "/") == -1) goto error; } /* Or allow a mount directory like "/sysroot/...". */ if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') { - if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) + if (add_string (&ret, m->mnt_fsname) == -1) goto error; if (mp && - add_string (&ret, &size, &alloc, &m->mnt_dir[sysroot_len]) == -1) + add_string (&ret, &m->mnt_dir[sysroot_len]) == -1) goto error; } } endmntent (fp); - if (add_string (&ret, &size, &alloc, NULL) == -1) + if (end_stringsbuf (&ret) == -1) return NULL; /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */ - for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) { - if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) { + for (i = 0; ret.argv[i] != NULL; i += mp ? 2 : 1) { + if (STRPREFIX (ret.argv[i], "/dev/mapper/") || + STRPREFIX (ret.argv[i], "/dev/dm-")) { char *canonical; - r = lv_canonical (ret[i], &canonical); + r = lv_canonical (ret.argv[i], &canonical); if (r == -1) { - free_strings (ret); + free_stringslen (ret.argv, ret.size); return NULL; } if (r == 1) { - free (ret[i]); - ret[i] = canonical; + free (ret.argv[i]); + ret.argv[i] = canonical; } /* Ignore the case where r == 0. This might happen where * eg. a LUKS /dev/mapper device is mounted, but that won't @@ -285,7 +285,7 @@ mounts_or_mountpoints (int mp) } } - return ret; + return ret.argv; } char ** @@ -325,8 +325,7 @@ do_umount_all (void) { FILE *fp; struct mntent *m; - char **mounts = NULL; - int size = 0, alloc = 0; + DECLARE_STRINGSBUF (mounts); char *err; int i, r; @@ -342,14 +341,14 @@ do_umount_all (void) while ((m = getmntent (fp)) != NULL) { /* Allow a mount directory like "/sysroot". */ if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) { - if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) { + if (add_string (&mounts, m->mnt_dir) == -1) { endmntent (fp); return -1; } } /* Or allow a mount directory like "/sysroot/...". */ if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') { - if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) { + if (add_string (&mounts, m->mnt_dir) == -1) { endmntent (fp); return -1; } @@ -358,21 +357,22 @@ do_umount_all (void) endmntent (fp); - qsort (mounts, size, sizeof (char *), compare_longest_first); + if (mounts.size > 0) + qsort (mounts.argv, mounts.size, sizeof (char *), compare_longest_first); /* Unmount them. */ - for (i = 0; i < size; ++i) { - r = command (NULL, &err, "umount", mounts[i], NULL); + for (i = 0; i < mounts.size; ++i) { + r = command (NULL, &err, "umount", mounts.argv[i], NULL); if (r == -1) { - reply_with_error ("umount: %s: %s", mounts[i], err); + reply_with_error ("umount: %s: %s", mounts.argv[i], err); free (err); - free_stringslen (mounts, size); + free_stringslen (mounts.argv, mounts.size); return -1; } free (err); } - free_stringslen (mounts, size); + free_stringslen (mounts.argv, mounts.size); return 0; } -- 1.7.9.1
Richard W.M. Jones
2012-Mar-13 08:26 UTC
[Libguestfs] [PATCH 2/2] Replace 'int' with 'size_t' passim.
From: "Richard W.M. Jones" <rjones at redhat.com> Analyze all uses of 'int' in the code, and replace with 'size_t' where appropriate. --- daemon/augeas.c | 2 +- daemon/checksum.c | 2 +- daemon/daemon.h | 2 +- daemon/debug.c | 68 ++++++++++++++++++++-------------------- daemon/ext2.c | 6 ++-- daemon/file.c | 5 ++- daemon/find.c | 9 +++-- daemon/guestfsd.c | 16 +++++---- daemon/lvm.c | 5 ++- daemon/mount.c | 3 +- daemon/proto.c | 6 ++-- daemon/readdir.c | 2 +- daemon/xattr.c | 14 ++++---- fish/fish.c | 24 +++++++------- fish/fish.h | 2 +- fish/glob.c | 8 ++-- fish/inspect.c | 2 +- fish/progress.c | 9 +++-- fish/rc.c | 3 +- fish/tilde.c | 2 +- fuse/guestmount.c | 2 +- generator/generator_java.ml | 4 +- generator/generator_python.ml | 6 ++-- inspector/virt-inspector.c | 8 ++-- ocaml/guestfs_c.c | 4 +- src/proto.c | 2 +- 26 files changed, 113 insertions(+), 103 deletions(-) diff --git a/daemon/augeas.c b/daemon/augeas.c index 4a09f57..f03b819 100644 --- a/daemon/augeas.c +++ b/daemon/augeas.c @@ -339,7 +339,7 @@ do_aug_ls (const char *path) { char **matches; char *buf; - int len; + size_t len; NEED_AUG (NULL); diff --git a/daemon/checksum.c b/daemon/checksum.c index 684f83f..be460f8 100644 --- a/daemon/checksum.c +++ b/daemon/checksum.c @@ -58,7 +58,7 @@ checksum (const char *csumtype, int fd) const char *program; char *out, *err; int flags, r; - int len; + size_t len; program = program_of_csum (csumtype); if (program == NULL) { diff --git a/daemon/daemon.h b/daemon/daemon.h index b973a7f..f3e77da 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -191,7 +191,7 @@ extern int cancel_receive (void); * reply, then send_file_* for each FileOut parameter. * Note max write size if GUESTFS_MAX_CHUNK_SIZE. */ -extern int send_file_write (const void *buf, int len); +extern int send_file_write (const void *buf, size_t len); extern int send_file_end (int cancel); /* only call this if there is a FileOut parameter */ diff --git a/daemon/debug.c b/daemon/debug.c index 4036af6..4e8d662 100644 --- a/daemon/debug.c +++ b/daemon/debug.c @@ -47,21 +47,21 @@ struct cmd { const char *cmd; - char * (*f) (const char *subcmd, int argc, char *const *const argv); + char * (*f) (const char *subcmd, size_t argc, char *const *const argv); }; -static char *debug_help (const char *subcmd, int argc, char *const *const argv); -static char *debug_binaries (const char *subcmd, int argc, char *const *const argv); -static char *debug_core_pattern (const char *subcmd, int argc, char *const *const argv); -static char *debug_env (const char *subcmd, int argc, char *const *const argv); -static char *debug_fds (const char *subcmd, int argc, char *const *const argv); -static char *debug_ldd (const char *subcmd, int argc, char *const *const argv); -static char *debug_ls (const char *subcmd, int argc, char *const *const argv); -static char *debug_ll (const char *subcmd, int argc, char *const *const argv); -static char *debug_progress (const char *subcmd, int argc, char *const *const argv); -static char *debug_qtrace (const char *subcmd, int argc, char *const *const argv); -static char *debug_segv (const char *subcmd, int argc, char *const *const argv); -static char *debug_sh (const char *subcmd, int argc, char *const *const argv); +static char *debug_help (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_binaries (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_core_pattern (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_env (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_fds (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_ldd (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_ls (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_ll (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_progress (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_qtrace (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_segv (const char *subcmd, size_t argc, char *const *const argv); +static char *debug_sh (const char *subcmd, size_t argc, char *const *const argv); static struct cmd cmds[] = { { "help", debug_help }, @@ -82,7 +82,7 @@ static struct cmd cmds[] = { char * do_debug (const char *subcmd, char *const *argv) { - int argc, i; + size_t argc, i; for (i = argc = 0; argv[i] != NULL; ++i) argc++; @@ -97,9 +97,9 @@ do_debug (const char *subcmd, char *const *argv) } static char * -debug_help (const char *subcmd, int argc, char *const *const argv) +debug_help (const char *subcmd, size_t argc, char *const *const argv) { - int len, i; + size_t len, i; char *r, *p; r = strdup ("Commands supported:"); @@ -128,7 +128,7 @@ debug_help (const char *subcmd, int argc, char *const *const argv) /* Show open FDs. */ static char * -debug_fds (const char *subcmd, int argc, char *const *const argv) +debug_fds (const char *subcmd, size_t argc, char *const *const argv) { int r; char *out; @@ -196,7 +196,7 @@ debug_fds (const char *subcmd, int argc, char *const *const argv) /* Force a segfault in the daemon. */ static char * -debug_segv (const char *subcmd, int argc, char *const *const argv) +debug_segv (const char *subcmd, size_t argc, char *const *const argv) { /* http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html * "Dereferencing a NULL Pointer: contrary to popular belief, @@ -214,16 +214,16 @@ debug_segv (const char *subcmd, int argc, char *const *const argv) * because it's not using the guest shell, and is not chrooted. */ static char * -debug_sh (const char *subcmd, int argc, char *const *const argv) +debug_sh (const char *subcmd, size_t argc, char *const *const argv) { + char *cmd; + size_t len, i, j; + if (argc < 1) { reply_with_error ("sh: expecting a command to run"); return NULL; } - char *cmd; - int len, i, j; - /* guestfish splits the parameter(s) into a list of strings, * and we have to reassemble them here. Not ideal. XXX */ @@ -268,7 +268,7 @@ debug_sh (const char *subcmd, int argc, char *const *const argv) /* Print the environment that commands get (by running external printenv). */ static char * -debug_env (const char *subcmd, int argc, char *const *const argv) +debug_env (const char *subcmd, size_t argc, char *const *const argv) { int r; char *out, *err; @@ -290,7 +290,7 @@ debug_env (const char *subcmd, int argc, char *const *const argv) * See tests/regressions/rhbz727178.sh */ static char * -debug_binaries (const char *subcmd, int argc, char *const *const argv) +debug_binaries (const char *subcmd, size_t argc, char *const *const argv) { int r; char *out, *err; @@ -318,7 +318,7 @@ debug_binaries (const char *subcmd, int argc, char *const *const argv) * See tests/regressions/rhbz727178.sh */ static char * -debug_ldd (const char *subcmd, int argc, char *const *const argv) +debug_ldd (const char *subcmd, size_t argc, char *const *const argv) { int r; char *out, *err, *ret; @@ -359,11 +359,11 @@ debug_ldd (const char *subcmd, int argc, char *const *const argv) /* List files in the appliance. */ static char * -debug_ls (const char *subcmd, int argc, char *const *const argv) +debug_ls (const char *subcmd, size_t argc, char *const *const argv) { - int len = count_strings (argv); + size_t len = count_strings (argv); const char *cargv[len+3]; - int i; + size_t i; cargv[0] = "ls"; cargv[1] = "-a"; @@ -389,11 +389,11 @@ debug_ls (const char *subcmd, int argc, char *const *const argv) /* List files in the appliance. */ static char * -debug_ll (const char *subcmd, int argc, char *const *const argv) +debug_ll (const char *subcmd, size_t argc, char *const *const argv) { - int len = count_strings (argv); + size_t len = count_strings (argv); const char *cargv[len+3]; - int i; + size_t i; cargv[0] = "ls"; cargv[1] = "-la"; @@ -419,7 +419,7 @@ debug_ll (const char *subcmd, int argc, char *const *const argv) /* Generate progress notification messages in order to test progress bars. */ static char * -debug_progress (const char *subcmd, int argc, char *const *const argv) +debug_progress (const char *subcmd, size_t argc, char *const *const argv) { if (argc < 1) { error: @@ -455,7 +455,7 @@ debug_progress (const char *subcmd, int argc, char *const *const argv) * crashes doesn't chroot. */ static char * -debug_core_pattern (const char *subcmd, int argc, char *const *const argv) +debug_core_pattern (const char *subcmd, size_t argc, char *const *const argv) { if (argc < 1) { reply_with_error ("core_pattern: expecting a core pattern"); @@ -510,7 +510,7 @@ write_cb (void *fd_ptr, const void *buf, size_t len) * directory in the libguestfs source tree. */ static char * -debug_qtrace (const char *subcmd, int argc, char *const *const argv) +debug_qtrace (const char *subcmd, size_t argc, char *const *const argv) { int enable; diff --git a/daemon/ext2.c b/daemon/ext2.c index 5defbc8..99d4574 100644 --- a/daemon/ext2.c +++ b/daemon/ext2.c @@ -463,7 +463,7 @@ do_mke2fs_J (const char *fstype, int blocksize, const char *device, char blocksize_s[32]; snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize); - int len = strlen (journal); + size_t len = strlen (journal); char jdev[len+32]; snprintf (jdev, len+32, "device=%s", journal); @@ -500,7 +500,7 @@ do_mke2fs_JL (const char *fstype, int blocksize, const char *device, char blocksize_s[32]; snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize); - int len = strlen (label); + size_t len = strlen (label); char jdev[len+32]; snprintf (jdev, len+32, "device=LABEL=%s", label); @@ -531,7 +531,7 @@ do_mke2fs_JU (const char *fstype, int blocksize, const char *device, char blocksize_s[32]; snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize); - int len = strlen (uuid); + size_t len = strlen (uuid); char jdev[len+32]; snprintf (jdev, len+32, "device=UUID=%s", uuid); diff --git a/daemon/file.c b/daemon/file.c index 161ea31..ef7b50c 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -89,7 +89,8 @@ char * do_cat (const char *path) { int fd; - int alloc, size, r, max; + size_t alloc, size, max; + ssize_t r; char *buf, *buf2; CHROOT_IN; @@ -671,7 +672,7 @@ do_file (const char *path) char * do_zfile (const char *method, const char *path) { - int len; + size_t len; const char *zcat; char *cmd; FILE *fp; diff --git a/daemon/find.c b/daemon/find.c index 014712e..833a391 100644 --- a/daemon/find.c +++ b/daemon/find.c @@ -31,9 +31,10 @@ #include "actions.h" static int -input_to_nul (FILE *fp, char *buf, int maxlen) +input_to_nul (FILE *fp, char *buf, size_t maxlen) { - int i = 0, c; + size_t i = 0; + int c; while (i < maxlen) { c = fgetc (fp); @@ -52,7 +53,9 @@ char ** do_find (const char *dir) { struct stat statbuf; - int r, len, sysrootdirlen; + int r; + size_t sysrootdirlen; + size_t len; char *cmd; FILE *fp; DECLARE_STRINGSBUF (ret); diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 6174074..43f41df 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -364,7 +364,7 @@ char * sysroot_path (const char *path) { char *r; - int len = strlen (path) + sysroot_len + 1; + size_t len = strlen (path) + sysroot_len + 1; r = malloc (len); if (r == NULL) @@ -497,7 +497,7 @@ sort_strings (char **argv, size_t len) void free_strings (char **argv) { - int argc; + size_t argc; for (argc = 0; argv[argc] != NULL; ++argc) free (argv[argc]); @@ -523,7 +523,8 @@ commandf (char **stdoutput, char **stderror, int flags, const char *name, ...) va_list args; const char **argv; char *s; - int i, r; + size_t i; + int r; /* Collect the command line arguments into an array. */ i = 2; @@ -655,7 +656,7 @@ int commandrvf (char **stdoutput, char **stderror, int flags, char const* const *argv) { - int so_size = 0, se_size = 0; + size_t so_size = 0, se_size = 0; int so_fd[2], se_fd[2]; int flag_copy_stdin = flags & COMMAND_FLAG_CHROOT_COPY_FILE_TO_STDIN; int stdin_fd[2] = { -1, -1 }; @@ -894,9 +895,10 @@ commandrvf (char **stdoutput, char **stderror, int flags, *stderror = q; if (*stderror) { (*stderror)[se_size] = '\0'; - se_size--; - while (se_size >= 0 && (*stderror)[se_size] == '\n') - (*stderror)[se_size--] = '\0'; + while (se_size > 0 && (*stderror)[se_size-1] == '\n') { + se_size--; + (*stderror)[se_size] = '\0'; + } } } diff --git a/daemon/lvm.c b/daemon/lvm.c index 8b56921..7519756 100644 --- a/daemon/lvm.c +++ b/daemon/lvm.c @@ -45,8 +45,8 @@ static char ** convert_lvm_output (char *out, const char *prefix) { char *p, *pend; - int len; DECLARE_STRINGSBUF (ret); + size_t len; char buf[256]; char *str; @@ -317,7 +317,8 @@ int do_lvm_remove_all (void) { char **xs; - int i, r; + size_t i; + int r; char *err; /* Remove LVs. */ diff --git a/daemon/mount.c b/daemon/mount.c index 5e74ce8..293353d 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -327,7 +327,8 @@ do_umount_all (void) struct mntent *m; DECLARE_STRINGSBUF (mounts); char *err; - int i, r; + size_t i; + int r; /* NB: Eventually we should aim to parse /proc/self/mountinfo, but * that requires custom parsing code. diff --git a/daemon/proto.c b/daemon/proto.c index ac62a6b..4bbe8f8 100644 --- a/daemon/proto.c +++ b/daemon/proto.c @@ -71,7 +71,7 @@ static struct timeval start_t; static struct timeval last_progress_t; /* Counts the number of progress notifications sent during this call. */ -static int count_progress; +static size_t count_progress; /* The daemon communications socket. */ static int sock; @@ -477,13 +477,13 @@ static int send_chunk (const guestfs_chunk *); /* Also check if the library sends us a cancellation message. */ int -send_file_write (const void *buf, int len) +send_file_write (const void *buf, size_t len) { guestfs_chunk chunk; int cancel; if (len > GUESTFS_MAX_CHUNK_SIZE) { - fprintf (stderr, "guestfsd: send_file_write: len (%d) > GUESTFS_MAX_CHUNK_SIZE (%d)\n", + fprintf (stderr, "guestfsd: send_file_write: len (%zu) > GUESTFS_MAX_CHUNK_SIZE (%d)\n", len, GUESTFS_MAX_CHUNK_SIZE); return -1; } diff --git a/daemon/readdir.c b/daemon/readdir.c index b8f5fea..f0ddd21 100644 --- a/daemon/readdir.c +++ b/daemon/readdir.c @@ -34,7 +34,7 @@ do_readdir (const char *path) guestfs_int_dirent v; DIR *dir; struct dirent *d; - int i; + size_t i; ret = malloc (sizeof *ret); if (ret == NULL) { diff --git a/daemon/xattr.c b/daemon/xattr.c index 92d0cf1..ca1b722 100644 --- a/daemon/xattr.c +++ b/daemon/xattr.c @@ -120,7 +120,7 @@ getxattrs (const char *path, { ssize_t len, vlen; char *buf = NULL; - int i, j; + size_t i, j; guestfs_int_xattr_list *r = NULL; CHROOT_IN; @@ -155,7 +155,7 @@ getxattrs (const char *path, * len. First count the strings. */ r->guestfs_int_xattr_list_len = 0; - for (i = 0; i < len; i += strlen (&buf[i]) + 1) + for (i = 0; i < (size_t) len; i += strlen (&buf[i]) + 1) r->guestfs_int_xattr_list_len++; r->guestfs_int_xattr_list_val @@ -165,7 +165,7 @@ getxattrs (const char *path, goto error; } - for (i = 0, j = 0; i < len; i += strlen (&buf[i]) + 1, ++j) { + for (i = 0, j = 0; i < (size_t) len; i += strlen (&buf[i]) + 1, ++j) { CHROOT_IN; vlen = getxattr (path, &buf[i], NULL, 0); CHROOT_OUT; @@ -203,7 +203,7 @@ getxattrs (const char *path, free (buf); if (r) { if (r->guestfs_int_xattr_list_val) { - unsigned int k; + size_t k; for (k = 0; k < r->guestfs_int_xattr_list_len; ++k) { free (r->guestfs_int_xattr_list_val[k].attrname); free (r->guestfs_int_xattr_list_val[k].attrval.attrval_val); @@ -260,7 +260,7 @@ do_lxattrlist (const char *path, char *const *names) char pathname[PATH_MAX]; size_t path_len = strlen (path); guestfs_int_xattr_list *ret = NULL; - int i, j; + size_t i, j; size_t k, m, nr_attrs; ssize_t len, vlen; char *buf = NULL; @@ -339,7 +339,7 @@ do_lxattrlist (const char *path, char *const *names) * len. First count the strings. */ nr_attrs = 0; - for (i = 0; i < len; i += strlen (&buf[i]) + 1) + for (i = 0; i < (size_t) len; i += strlen (&buf[i]) + 1) nr_attrs++; newptr @@ -363,7 +363,7 @@ do_lxattrlist (const char *path, char *const *names) entry[m].attrval.attrval_val = NULL; } - for (i = 0, j = 0; i < len; i += strlen (&buf[i]) + 1, ++j) { + for (i = 0, j = 0; i < (size_t) len; i += strlen (&buf[i]) + 1, ++j) { CHROOT_IN; vlen = lgetxattr (pathname, &buf[i], NULL, 0); CHROOT_OUT; diff --git a/fish/fish.c b/fish/fish.c index 8e3e9fb..8bfea32 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -60,9 +60,9 @@ static int launch (void); static void interactive (void); static void shell_script (void); static void script (int prompt); -static void cmdline (char *argv[], int optind, int argc); +static void cmdline (char *argv[], size_t optind, size_t argc); static struct parsed_command parse_command_line (char *buf, int *exit_on_error_rtn); -static int parse_quoted_string (char *p); +static ssize_t parse_quoted_string (char *p); static int execute_and_inline (const char *cmd, int exit_on_error); static void error_cb (guestfs_h *g, void *data, const char *msg); static void initialize_readline (void); @@ -624,7 +624,7 @@ rl_gets (int prompt) #endif /* HAVE_LIBREADLINE */ static char buf[8192]; - int len; + size_t len; if (prompt) printf (FISH); line_read = fgets (buf, sizeof buf, stdin); @@ -701,7 +701,7 @@ parse_command_line (char *buf, int *exit_on_error_rtn) { struct parsed_command pcmd; char *p, *pend; - int len; + ssize_t len; int tilde_candidate; int r; const size_t argv_len = sizeof pcmd.argv / sizeof pcmd.argv[0]; @@ -880,7 +880,7 @@ hexdigit (char d) * with the true character. Since the string is returned in place, * the escapes must make the string shorter. */ -static int +static ssize_t parse_quoted_string (char *p) { char *start = p; @@ -991,7 +991,7 @@ execute_and_inline (const char *cmd, int global_exit_on_error) } static void -cmdline (char *argv[], int optind, int argc) +cmdline (char *argv[], size_t optind, size_t argc) { const char *cmd; char **params; @@ -1040,7 +1040,7 @@ int issue_command (const char *cmd, char *argv[], const char *pipecmd, int rc_exit_on_error_flag) { - int argc; + size_t argc; int stdout_saved_fd = -1; int pid = 0; int r; @@ -1208,17 +1208,17 @@ error_cb (guestfs_h *g, void *data, const char *msg) void free_strings (char **argv) { - int argc; + size_t argc; for (argc = 0; argv[argc] != NULL; ++argc) free (argv[argc]); free (argv); } -int +size_t count_strings (char *const *argv) { - int c; + size_t c; for (c = 0; argv[c]; ++c) ; @@ -1228,7 +1228,7 @@ count_strings (char *const *argv) void print_strings (char *const *argv) { - int argc; + size_t argc; for (argc = 0; argv[argc] != NULL; ++argc) printf ("%s\n", argv[argc]); @@ -1237,7 +1237,7 @@ print_strings (char *const *argv) void print_table (char *const *argv) { - int i; + size_t i; for (i = 0; argv[i] != NULL; i += 2) printf ("%s: %s\n", argv[i], argv[i+1]); diff --git a/fish/fish.h b/fish/fish.h index 668202d..b4b05fe 100644 --- a/fish/fish.h +++ b/fish/fish.h @@ -69,7 +69,7 @@ extern int issue_command (const char *cmd, char *argv[], const char *pipe, int r extern void list_builtin_commands (void); extern int display_builtin_command (const char *cmd); extern void free_strings (char **argv); -extern int count_strings (char *const *argv); +extern size_t count_strings (char *const *argv); extern void print_strings (char *const *argv); extern void print_table (char *const *argv); extern int is_true (const char *str); diff --git a/fish/glob.c b/fish/glob.c index 0141040..108eda3 100644 --- a/fish/glob.c +++ b/fish/glob.c @@ -28,7 +28,7 @@ /* A bit tricky because in the case where there are multiple * paths we have to perform a Cartesian product. */ -static void glob_issue (char *cmd, size_t argc, char ***globs, int *posn, int *count, int *r); +static void glob_issue (char *cmd, size_t argc, char ***globs, size_t *posn, size_t *count, int *r); int run_glob (const char *cmd, size_t argc, char *argv[]) @@ -44,8 +44,8 @@ run_glob (const char *cmd, size_t argc, char *argv[]) * argv[1-]. */ char **globs[argc]; - int posn[argc]; - int count[argc]; + size_t posn[argc]; + size_t count[argc]; size_t i; int r = 0; @@ -131,7 +131,7 @@ run_glob (const char *cmd, size_t argc, char *argv[]) static void glob_issue (char *cmd, size_t argc, - char ***globs, int *posn, int *count, + char ***globs, size_t *posn, size_t *count, int *r) { size_t i; diff --git a/fish/inspect.c b/fish/inspect.c index f93367b..3358cd4 100644 --- a/fish/inspect.c +++ b/fish/inspect.c @@ -36,7 +36,7 @@ static char *root = NULL; static void free_strings (char **argv) { - int argc; + size_t argc; for (argc = 0; argv[argc] != NULL; ++argc) free (argv[argc]); diff --git a/fish/progress.c b/fish/progress.c index 6c30e95..c296f40 100644 --- a/fish/progress.c +++ b/fish/progress.c @@ -88,7 +88,7 @@ rmsd_get_standard_deviation (const struct rmsd *r) struct progress_bar { double start; /* start time of command */ - int count; /* number of progress notifications per cmd */ + size_t count; /* number of progress notifications per cmd */ struct rmsd rmsd; /* running mean and standard deviation */ int have_terminfo; int utf8_mode; @@ -154,7 +154,7 @@ progress_bar_reset (struct progress_bar *bar) } static const char * -spinner (struct progress_bar *bar, int count) +spinner (struct progress_bar *bar, size_t count) { /* Choice of unicode spinners. * @@ -262,7 +262,8 @@ void progress_bar_set (struct progress_bar *bar, uint64_t position, uint64_t total) { - int i, cols, pulse_mode; + size_t i, cols; + int pulse_mode; double ratio; const char *s_open, *s_dot, *s_dash, *s_close; @@ -307,7 +308,7 @@ progress_bar_set (struct progress_bar *bar, fputs (s_open, stdout); if (!pulse_mode) { - int dots = ratio * (double) (cols - COLS_OVERHEAD); + size_t dots = ratio * (double) (cols - COLS_OVERHEAD); for (i = 0; i < dots; ++i) fputs (s_dot, stdout); diff --git a/fish/rc.c b/fish/rc.c index 4c119f7..52a2623 100644 --- a/fish/rc.c +++ b/fish/rc.c @@ -36,7 +36,8 @@ #include "rc_protocol.h" static void -create_sockpath (pid_t pid, char *sockpath, int len, struct sockaddr_un *addr) +create_sockpath (pid_t pid, char *sockpath, size_t len, + struct sockaddr_un *addr) { char dir[128]; uid_t euid = geteuid (); diff --git a/fish/tilde.c b/fish/tilde.c index 8ee1437..9267966 100644 --- a/fish/tilde.c +++ b/fish/tilde.c @@ -78,7 +78,7 @@ static char * expand_home (char *orig, const char *append) { const char *home; - int len; + size_t len; char *str; home = getenv ("HOME"); diff --git a/fuse/guestmount.c b/fuse/guestmount.c index a9bf4e3..f6c3463 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -990,7 +990,7 @@ main (int argc, char *argv[]) int option_index; struct sigaction sa; - int fuse_argc = 0; + size_t fuse_argc = 0; const char **fuse_argv = NULL; #define ADD_FUSE_ARG(str) \ diff --git a/generator/generator_java.ml b/generator/generator_java.ml index 3a13052..d95bd59 100644 --- a/generator/generator_java.ml +++ b/generator/generator_java.ml @@ -683,7 +683,7 @@ and generate_java_struct_return typ jtyp cols pr " }\n"; | name, FBuffer -> pr " {\n"; - pr " int len = r->%s_len;\n" name; + pr " size_t len = r->%s_len;\n" name; pr " char s[len+1];\n"; pr " memcpy (s, r->%s, len);\n" name; pr " s[len] = 0;\n"; @@ -726,7 +726,7 @@ and generate_java_struct_list_return typ jtyp cols pr " }\n"; | name, FBuffer -> pr " {\n"; - pr " int len = r->val[i].%s_len;\n" name; + pr " size_t len = r->val[i].%s_len;\n" name; pr " char s[len+1];\n"; pr " memcpy (s, r->val[i].%s, len);\n" name; pr " s[len] = 0;\n"; diff --git a/generator/generator_python.ml b/generator/generator_python.ml index 7d8513a..fbf3151 100644 --- a/generator/generator_python.ml +++ b/generator/generator_python.ml @@ -89,7 +89,7 @@ static PyObject * put_string_list (char * const * const argv) { PyObject *list; - int argc, i; + size_t argc, i; for (argc = 0; argv[argc] != NULL; ++argc) ; @@ -110,7 +110,7 @@ static PyObject * put_table (char * const * const argv) { PyObject *list, *item; - int argc, i; + size_t argc, i; for (argc = 0; argv[argc] != NULL; ++argc) ; @@ -134,7 +134,7 @@ put_table (char * const * const argv) static void free_strings (char **argv) { - int argc; + size_t argc; for (argc = 0; argv[argc] != NULL; ++argc) free (argv[argc]); diff --git a/inspector/virt-inspector.c b/inspector/virt-inspector.c index 269e51d..103a61e 100644 --- a/inspector/virt-inspector.c +++ b/inspector/virt-inspector.c @@ -62,7 +62,7 @@ static void output_drive_mappings (xmlTextWriterPtr xo, char *root); static void output_applications (xmlTextWriterPtr xo, char *root); static void canonicalize (char *dev); static void free_strings (char **argv); -static int count_strings (char *const*argv); +static size_t count_strings (char *const*argv); static void do_xpath (const char *query); static inline char * @@ -790,17 +790,17 @@ canonicalize (char *dev) static void free_strings (char **argv) { - int argc; + size_t argc; for (argc = 0; argv[argc] != NULL; ++argc) free (argv[argc]); free (argv); } -static int +static size_t count_strings (char *const *argv) { - int c; + size_t c; for (c = 0; argv[c]; ++c) ; diff --git a/ocaml/guestfs_c.c b/ocaml/guestfs_c.c index c4b2a96..cfbe1cc 100644 --- a/ocaml/guestfs_c.c +++ b/ocaml/guestfs_c.c @@ -190,7 +190,7 @@ ocaml_guestfs_strings_val (guestfs_h *g, value sv) { CAMLparam1 (sv); char **r; - unsigned int i; + size_t i; r = guestfs_safe_malloc (g, sizeof (char *) * (Wosize_val (sv) + 1)); for (i = 0; i < Wosize_val (sv); ++i) @@ -204,7 +204,7 @@ ocaml_guestfs_strings_val (guestfs_h *g, value sv) void ocaml_guestfs_free_strings (char **argv) { - unsigned int i; + size_t i; for (i = 0; argv[i] != NULL; ++i) free (argv[i]); diff --git a/src/proto.c b/src/proto.c index 1ab8d18..c9ddaca 100644 --- a/src/proto.c +++ b/src/proto.c @@ -241,7 +241,7 @@ static int read_log_message_or_eof (guestfs_h *g, int fd, int error_if_eof) { char buf[BUFSIZ]; - int n; + ssize_t n; #if 0 debug (g, "read_log_message_or_eof: %p g->state = %d, fd = %d", -- 1.7.9.1
Reasonably Related Threads
- [PATCH 1/2] daemon: free the string on stringsbuf add failure
- [PATCH 1/4] daemon: introduce free_stringsbuf
- [PATCH 00/13] Fix errors found using Coverity static analyzer.
- [PATCH 0/6 v2] [FOR COMMENTS ONLY] Rework inspection.
- [PATCH v3 1/2] Modify the function: analyze_line, make it more flexible