From: Richard Jones Date: Thu, 3 Jun 2010 13:01:18 +0000 (+0100) Subject: mkfs-b: Check that blocksize parameter is > 0 and a power of 2. X-Git-Tag: 1.3.19~7 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=0606cb54671e10f104b4eba1c70378ef8725322b;p=libguestfs.git mkfs-b: Check that blocksize parameter is > 0 and a power of 2. --- diff --git a/daemon/daemon.h b/daemon/daemon.h index d90b65c..55f7b08 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -48,6 +48,8 @@ extern void sort_strings (char **argv, int len); extern void free_strings (char **argv); extern void free_stringslen (char **argv, int len); +extern int is_power_of_2 (unsigned long v); + #define command(out,err,name,...) commandf((out),(err),0,(name),__VA_ARGS__) #define commandr(out,err,name,...) commandrf((out),(err),0,(name),__VA_ARGS__) #define commandv(out,err,argv) commandvf((out),(err),0,(argv)) diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index f9e5a68..49aca08 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -554,6 +554,13 @@ count_strings (char *const *argv) return argc; } +/* http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 */ +int +is_power_of_2 (unsigned long v) +{ + return v && ((v & (v - 1)) == 0); +} + static int compare (const void *vp1, const void *vp2) { diff --git a/daemon/mkfs.c b/daemon/mkfs.c index 6735d24..2c3a3cd 100644 --- a/daemon/mkfs.c +++ b/daemon/mkfs.c @@ -104,6 +104,11 @@ do_mkfs_b (const char *fstype, int blocksize, const char *device) const char *extra[2]; char blocksize_s[32]; + if (blocksize <= 0 || !is_power_of_2 (blocksize)) { + reply_with_error ("block size must be > 0 and a power of 2"); + return -1; + } + snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize); extra[0] = "-b";