Skip to contentSkip to conversation

blkdebug: Don't leak bs->file on failure

PATCH In review Kevin Wolf 2 participants 2 messages Feb 8, 2014 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH v2 1/1 blkdebug: Don't leak bs->file on failure

---
 block/blkdebug.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
block/blkdebug.c +9 -5
index 56c4cd0..8eb0db0 100644
@@ -396,14 +396,14 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
     if (error_is_set(&local_err)) {
         error_propagate(errp, local_err);
         ret = -EINVAL;
-        goto fail;
+        goto out;
     }
 
     /* Read rules from config file or command line options */
     config = qemu_opt_get(opts, "config");
     ret = read_config(s, config, options, errp);
     if (ret) {
-        goto fail;
+        goto out;
     }
 
     /* Set initial state */
@@ -414,7 +414,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
                           flags, true, false, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
-        goto fail;
+        goto out;
     }
 
     /* Set request alignment */
@@ -424,11 +424,15 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
     } else {
         error_setg(errp, "Invalid alignment");
         ret = -EINVAL;
-        goto fail;
+        goto fail_unref;
     }
 
     ret = 0;
-fail:
+    goto out;
+
+fail_unref:
+    bdrv_unref(bs->file);
+out:
     qemu_opts_del(opts);
     return ret;
 }
Signature · 1 line
1.8.1.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 45 lines · Kevin Wolf
---
 block/blkdebug.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
block/blkdebug.c +4 -4
index 56c4cd0..8eb0db0 100644
@@ -396,14 +396,14 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
     if (error_is_set(&local_err)) {
         error_propagate(errp, local_err);
         ret = -EINVAL;
-        goto fail;
+        goto out;
     }
 
     /* Read rules from config file or command line options */
     config = qemu_opt_get(opts, "config");
     ret = read_config(s, config, options, errp);
     if (ret) {
-        goto fail;
+        goto out;
     }
 
     /* Set initial state */
@@ -414,7 +414,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
                           flags, true, false, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
-        goto fail;
+        goto out;
     }
 
     /* Set request alignment */
@@ -424,11 +424,15 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
     } else {
         error_setg(errp, "Invalid alignment");
         ret = -EINVAL;
-        goto fail;
+        goto fail_unref;
     }
 

block/blkdebug.c static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags, · line 438

Quoted 10 lines
block/blkdebug.c +5 -1
     ret = 0;
-fail:
+    goto out;
+
+fail_unref:
+    bdrv_unref(bs->file);
+out:
     qemu_opts_del(opts);
     return ret;
 }

I think this is correct, but the standalone "goto out" looks awful :)

In such cases, when I have to combine unconditional teardown with release-on-error-only, I apply a "nested ifs" pattern, where the release-on-error-only bits (on the way out) depend on the function exit status. But "nested ifs" is foreign for qemu, so I think these goto's actually match the qemu style.

Thanks!
Laszlo