Richard W.M. Jones
2010-Jun-03 13:55 UTC
[Libguestfs] [PATCH] mkfs-b: Check that blocksize parameter is > 0 and a power of 2.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into Xen guests. http://et.redhat.com/~rjones/virt-p2v -------------- next part -------------->From 0606cb54671e10f104b4eba1c70378ef8725322b Mon Sep 17 00:00:00 2001From: Richard Jones <rjones at redhat.com> Date: Thu, 3 Jun 2010 14:01:18 +0100 Subject: [PATCH 3/4] mkfs-b: Check that blocksize parameter is > 0 and a power of 2. --- daemon/daemon.h | 2 ++ daemon/guestfsd.c | 7 +++++++ daemon/mkfs.c | 5 +++++ 3 files changed, 14 insertions(+), 0 deletions(-) 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"; -- 1.6.6.1