Skip to contentSkip to conversation

qemu-img resize: Fix option parsing

PATCH In review Kevin Wolf 2 participants 2 messages Apr 29, 2011 – May 3, 2011

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 qemu-img resize: Fix option parsing

For shrinking images, you're supposed to use a negative size. However, the leading minus makes getopt think that it's an option and so you get the help text if you don't use -- like in 'qemu-img resize test.img -- -1G'.

This patch handles the size first and removes it from the argument list so that getopt won't even try to interpret it and you don't need -- any more.

---
 qemu-img.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)
qemu-img.c +11 -2
index ed5ba91..e825123 100644
@@ -1442,6 +1442,16 @@ static int img_resize(int argc, char **argv)
         { NULL }
     };
 
+    /* Remove size from argv manually so that negative numbers are not treated
+     * as options by getopt. */
+    if (argc < 3) {
+        help();
+        return 1;
+    }
+
+    size = argv[--argc];
+
+    /* Parse getopt arguments */
     fmt = NULL;
     for(;;) {
         c = getopt(argc, argv, "f:h");
@@ -1458,11 +1468,10 @@ static int img_resize(int argc, char **argv)
             break;
         }
     }
-    if (optind + 1 >= argc) {
+    if (optind >= argc) {
         help();
     }
     filename = argv[optind++];
-    size = argv[optind++];
 
     /* Choose grow, shrink, or absolute resize mode */
     switch (size[0]) {
Signature · 1 line
1.7.2.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Kevin Wolf

For shrinking images, you're supposed to use a negative size. However, the leading minus makes getopt think that it's an option and so you get the help text if you don't use -- like in 'qemu-img resize test.img -- -1G'.

This patch handles the size first and removes it from the argument list so that getopt won't even try to interpret it and you don't need -- any more.

---
 qemu-img.c |   13 +++++++++++--

 1 files changed, 11 insertions(+), 2 deletions(-)