Skip to contentSkip to conversation

qemu-img: Fail creation if backing format is invalid

PATCH Applied Stefan Hajnoczi 2 participants 6 messages Dec 7, 2010 – Dec 8, 2010

lore
Share & tools
lore
#0 lore raw

cover qemu-img: Fail creation if backing format is invalid

This patch series adds a check to validate the backing format before creating an image file. This ensures we provide a clear error message as early as possible when an unsupported format is used.

The first three patches clean up code on the way and the last patch makes the actual backing format validation change.

#1 lore raw

PATCH 1/4 qemu-option: Don't reinvent append_option_parameters()

parse_option_parameters() may need to create a new option parameter list from a template list. Use append_option_parameters() instead of duplicating the code.

---
 qemu-option.c |    9 +--------
 1 files changed, 1 insertions(+), 8 deletions(-)
qemu-option.c +1 -8
index 1f8f41a..e380fc1 100644
@@ -416,20 +416,13 @@ QEMUOptionParameter *parse_option_parameters(const char *param,
     char value[256];
     char *param_delim, *value_delim;
     char next_delim;
-    size_t num_options;
 
     if (list == NULL) {
         return NULL;
     }
 
     if (dest == NULL) {
-        // Count valid options
-        num_options = count_option_parameters(list);
-
-        // Create a copy of the option list to fill in values
-        dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
-        allocated = dest;
-        memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
+        dest = allocated = append_option_parameters(NULL, list);
     }
 
     while (*param) {
Signature · 1 line
1.7.2.3
#2 lore raw

PATCH 2/4 qemu-option: Fix parse_option_parameters() documentation typo

In reply to Stefan Hajnoczi

Yoda said, "list is the templace is". Fix this.

---
 qemu-option.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
qemu-option.c +2 -2
index e380fc1..65db542 100644
@@ -394,8 +394,8 @@ QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
 /*
  * Parses a parameter string (param) into an option list (dest).
  *
- * list is the templace is. If dest is NULL, a new copy of list is created for
- * it. If list is NULL, this function fails.
+ * list is the template option list. If dest is NULL, a new copy of list is
+ * created. If list is NULL, this function fails.
  *
  * A parameter string consists of one or more parameters, separated by commas.
  * Each parameter consists of its name and possibly of a value. In the latter
Signature · 1 line
1.7.2.3
#3 lore raw

PATCH 3/4 qemu-img: Free option parameter lists in img_create()

In reply to Stefan Hajnoczi

Free option parameter lists in the img_create() error return path.

---
 qemu-img.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
qemu-img.c +2 -2
index 5b6e648..23bb7dc 100644
@@ -439,8 +439,6 @@ static int img_create(int argc, char **argv)
     puts("");
 
     ret = bdrv_create(drv, filename, param);
-    free_option_parameters(create_options);
-    free_option_parameters(param);
 
     if (ret < 0) {
         if (ret == -ENOTSUP) {
@@ -452,6 +450,8 @@ static int img_create(int argc, char **argv)
         }
     }
 out:
+    free_option_parameters(create_options);
+    free_option_parameters(param);
     if (ret) {
         return 1;
     }
Signature · 1 line
1.7.2.3
#4 lore raw

PATCH 4/4 qemu-img: Fail creation if backing format is invalid

In reply to Stefan Hajnoczi

The qemu-img create command should check the backing format to ensure only image files with valid backing formats are created. By checking in qemu-img.c we can print a useful error message.

---
 qemu-img.c |   22 ++++++++++++----------
 1 files changed, 12 insertions(+), 10 deletions(-)
qemu-img.c +12 -10
index 23bb7dc..b10f363 100644
@@ -300,6 +300,7 @@ static int img_create(int argc, char **argv)
     const char *base_filename = NULL;
     BlockDriver *drv, *proto_drv;
     QEMUOptionParameter *param = NULL, *create_options = NULL;
+    QEMUOptionParameter *backing_fmt = NULL;
     char *options = NULL;
 
     flags = 0;
@@ -390,14 +391,22 @@ static int img_create(int argc, char **argv)
         goto out;
     }
 
+    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
+    if (backing_fmt && backing_fmt->value.s) {
+        if (!bdrv_find_format(backing_fmt->value.s)) {
+            error("Unknown backing file format '%s'",
+                  backing_fmt->value.s);
+            ret = -1;
+            goto out;
+        }
+    }
+
     // The size for the image must always be specified, with one exception:
     // If we are using a backing file, we can obtain the size from there
     if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
 
         QEMUOptionParameter *backing_file =
             get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
-        QEMUOptionParameter *backing_fmt =
-            get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
 
         if (backing_file && backing_file->value.s) {
             BlockDriverState *bs;
@@ -406,14 +415,7 @@ static int img_create(int argc, char **argv)
             char buf[32];
 
             if (backing_fmt && backing_fmt->value.s) {
-                 if (bdrv_find_format(backing_fmt->value.s)) {
-                     fmt = backing_fmt->value.s;
-                } else {
-                     error("Unknown backing file format '%s'",
-                        backing_fmt->value.s);
-                     ret = -1;
-                     goto out;
-                }
+                fmt = backing_fmt->value.s;
             }
 
             bs = bdrv_new_open(backing_file->value.s, fmt, BDRV_O_FLAGS);
Signature · 1 line
1.7.2.3
Applied
#5 lore raw
In reply to Stefan Hajnoczi

Am 07.12.2010 10:35, schrieb Stefan Hajnoczi:

Quoted 6 lines

This patch series adds a check to validate the backing format before creating an image file. This ensures we provide a clear error message as early as possible when an unsupported format is used.

The first three patches clean up code on the way and the last patch makes the actual backing format validation change.

Thanks, applied all to the block branch.

Kevin