mkfs-b: Check that blocksize parameter is > 0 and a power of 2.
authorRichard Jones <rjones@redhat.com>
Thu, 3 Jun 2010 13:01:18 +0000 (14:01 +0100)
committerRichard Jones <rjones@redhat.com>
Thu, 3 Jun 2010 13:33:59 +0000 (14:33 +0100)
daemon/daemon.h
daemon/guestfsd.c
daemon/mkfs.c

index d90b65c..55f7b08 100644 (file)
@@ -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 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))
 #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))
index f9e5a68..49aca08 100644 (file)
@@ -554,6 +554,13 @@ count_strings (char *const *argv)
   return argc;
 }
 
   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)
 {
 static int
 compare (const void *vp1, const void *vp2)
 {
index 6735d24..2c3a3cd 100644 (file)
@@ -104,6 +104,11 @@ do_mkfs_b (const char *fstype, int blocksize, const char *device)
   const char *extra[2];
   char blocksize_s[32];
 
   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";
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
 
   extra[0] = "-b";