Skip to contentSkip to conversation

qemu-img: error path fixes

PATCH Applied Stefan Hajnoczi 6 participants 14 messages Aug 26, 2014 – Aug 27, 2014

lore
Share & tools
lore
#0 lore raw

cover qemu-img: error path fixes

I noticed a few minor issues when looking at cache flag parsing. This series cleans them up.

Stefan Hajnoczi (3):
qemu-img: fix img_commit() error return value
qemu-img: fix img_compare() flags error path
qemu-img: always goto out in img_snapshot() error paths

 qemu-img.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/3 qemu-img: fix img_commit() error return value

The img_commit() return value is a process exit code. Use 1 for failure instead of -1. The other failure paths in this function already use 1.

---
 qemu-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-img.c +1 -1
index c843420..dc3adb5 100644
@@ -771,7 +771,7 @@ static int img_commit(int argc, char **argv)
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
         error_report("Invalid cache option: %s", cache);
-        return -1;
+        return 1;
     }
 
     bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
Signature · 1 line
1.9.3
Reviewed-by on v1 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu-img: fix img_commit() error return value

Quoted 7 lines · Stefan Hajnoczi

The img_commit() return value is a process exit code. Use 1 for failure instead of -1. The other failure paths in this function already use 1.

---
  qemu-img.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu-img: fix img_commit() error return value

In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi

The img_commit() return value is a process exit code. Use 1 for failure instead of -1. The other failure paths in this function already use 1.

---
 qemu-img.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-img.c +0 -0
index c843420..dc3adb5 100644

qemu-img.c static int img_commit(int argc, char **argv) · line 774

Quoted 5 lines
qemu-img.c +1 -1
@@ -771,7 +771,7 @@ static int img_commit(int argc, char **argv)
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
         error_report("Invalid cache option: %s", cache);
-        return -1;
+        return 1;

Nothing against this patch (you're consistent with the surrounding code, and most of qemu for that matter), but it highlights why I'm a fan of 'return EXIT_FAILURE' instead of 'return 1' in functions that return an exit status, because that makes it a lot more obvious _why_ I'm returning a non-negative number to represent failure.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu-img: fix img_commit() error return value

Quoted 9 lines · Eric Blake
Quoted 8 lines · Stefan Hajnoczi

The img_commit() return value is a process exit code. Use 1 for failure instead of -1. The other failure paths in this function already use 1.

---
  qemu-img.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines

qemu-img.c static int img_commit(int argc, char **argv) · line 774

Quoted 10 lines
qemu-img.c +1 -1
index c843420..dc3adb5 100644
@@ -771,7 +771,7 @@ static int img_commit(int argc, char **argv)
      ret = bdrv_parse_cache_flags(cache, &flags);
      if (ret < 0) {
          error_report("Invalid cache option: %s", cache);
-        return -1;
+        return 1;

Nothing against this patch (you're consistent with the surrounding code, and most of qemu for that matter), but it highlights why I'm a fan of 'return EXIT_FAILURE' instead of 'return 1' in functions that return an exit status, because that makes it a lot more obvious _why_ I'm returning a non-negative number to represent failure.

"Hey, good point."

jsnow@local ~/s/qemu> git grep 'return 1;' | wc -l 842

"oh."

This patch is probably fine -- is there some larger scheme we want to cook up within the style guide for advocating things like return code and error reporting standards?

It seems to me like the preferred style for errors and returns has changed several times and there's not a good concrete rule (written) at the moment. It might be worth touching the CODING_GUIDE and/or HACKING files with recommendations, if we can agree on some consistent set of rules, like getting rid of error_set(...) and not using positive, un-named integers to represent errors.

Signature · 1 line
—js
#5 lore raw

Re: [Qemu-devel] [PATCH 1/3] qemu-img: fix img_commit() error return value

Quoted 10 lines · John Snow
Quoted 9 lines · Eric Blake
Quoted 8 lines · Stefan Hajnoczi

The img_commit() return value is a process exit code. Use 1 for failure instead of -1. The other failure paths in this function already use 1.

---
  qemu-img.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
qemu-img.c +0 -0
index c843420..dc3adb5 100644
@@ -771,7 +771,7 @@ static int img_commit(int argc, char **argv)
      ret = bdrv_parse_cache_flags(cache, &flags);
      if (ret < 0) {
          error_report("Invalid cache option: %s", cache);
Quoted 8 lines

qemu-img.c static int img_commit(int argc, char **argv) · line 774

Quoted 2 lines
qemu-img.c +1 -1
-        return -1;
+        return 1;

Nothing against this patch (you're consistent with the surrounding code, and most of qemu for that matter), but it highlights why I'm a fan of 'return EXIT_FAILURE' instead of 'return 1' in functions that return an exit status, because that makes it a lot more obvious _why_ I'm returning a non-negative number to represent failure.

For me, EXIT_FAILURE carries a smell.

In main(), the fact that we're returning an exit code is trivial.

Elsewhere, if all you want to return is "succeeded / failed" information encoded as integer, stick to the usual 0/-1 convention, and leave mapping that to exit codes to callers.

If you need to return one of several error codes, EXIT_FAILURE is of no use.

Quoted 11 lines

"Hey, good point."

jsnow@local ~/s/qemu> git grep 'return 1;' | wc -l 842

"oh."

This patch is probably fine -- is there some larger scheme we want to cook up within the style guide for advocating things like return code and error reporting standards?

Quoted 6 lines

It seems to me like the preferred style for errors and returns has changed several times and there's not a good concrete rule (written) at the moment. It might be worth touching the CODING_GUIDE and/or HACKING files with recommendations, if we can agree on some consistent set of rules, like getting rid of error_set(...) and not using positive, un-named integers to represent errors.

Yes, we could use some written guidance on returning errors. I haven't found the time to write. When I can spare a bit of time for errors, I tend to invest it in killing obsolete error reporting interfaces, so I don't have to write guidance on them.

#6 lore raw

PATCH 2/3 qemu-img: fix img_compare() flags error path

In reply to Stefan Hajnoczi

If img_compare() fails to parse the cache flags the goto out3 code path will call qemu_progress_end(). Make sure we actually call qemu_progress_init() first.

---
 qemu-img.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
qemu-img.c +3 -3
index dc3adb5..01750b7 100644
@@ -1018,6 +1018,9 @@ static int img_compare(int argc, char **argv)
     filename1 = argv[optind++];
     filename2 = argv[optind++];
 
+    /* Initialize before goto out */
+    qemu_progress_init(progress, 2.0);
+
     flags = BDRV_O_FLAGS;
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
@@ -1026,9 +1029,6 @@ static int img_compare(int argc, char **argv)
         goto out3;
     }
 
-    /* Initialize before goto out */
-    qemu_progress_init(progress, 2.0);
-
     bs1 = bdrv_new_open("image 1", filename1, fmt1, flags, true, quiet);
     if (!bs1) {
         error_report("Can't open file %s", filename1);
Signature · 1 line
1.9.3
Reviewed-by on v1 2/3
#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] qemu-img: fix img_compare() flags error path

Quoted 9 lines · Stefan Hajnoczi

If img_compare() fails to parse the cache flags the goto out3 code path will call qemu_progress_end(). Make sure we actually call qemu_progress_init() first.

---
  qemu-img.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
#8 lore raw

PATCH 3/3 qemu-img: always goto out in img_snapshot() error paths

In reply to Stefan Hajnoczi

The out label has the qemu_progress_end() and other cleanup calls. Always goto out in error paths so the cleanup happens.

Note that bdrv_unref(NULL) is safe. We just need to initialize bs to NULL at the top of the function.

We can now remove the obsolete bs_old_backing = NULL and bs_new_backing = NULL for safe mode. Originally it was necessary in commit 3e85c6fd ("qemu-img rebase") but became useless in commit c2abcce ("qemu-img:
avoid calling exit(1) to release resources properly") because the variables are already initialized during declaration.

---
 qemu-img.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
qemu-img.c +6 -9
index 01750b7..ac2e21b 100644
@@ -2323,7 +2323,7 @@ static int img_snapshot(int argc, char **argv)
 
 static int img_rebase(int argc, char **argv)
 {
-    BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
+    BlockDriverState *bs = NULL, *bs_old_backing = NULL, *bs_new_backing = NULL;
     BlockDriver *old_backing_drv, *new_backing_drv;
     char *filename;
     const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg;
@@ -2395,14 +2395,14 @@ static int img_rebase(int argc, char **argv)
     ret = bdrv_parse_cache_flags(cache, &flags);
     if (ret < 0) {
         error_report("Invalid cache option: %s", cache);
-        return -1;
+        goto out;
     }
 
     src_flags = BDRV_O_FLAGS;
     ret = bdrv_parse_cache_flags(src_cache, &src_flags);
     if (ret < 0) {
         error_report("Invalid source cache option: %s", src_cache);
-        return -1;
+        goto out;
     }
 
     /*
@@ -2413,7 +2413,8 @@ static int img_rebase(int argc, char **argv)
      */
     bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
     if (!bs) {
-        return 1;
+        ret = -1;
+        goto out;
     }
 
     /* Find the right drivers for the backing files */
@@ -2439,11 +2440,7 @@ static int img_rebase(int argc, char **argv)
     }
 
     /* For safe rebasing we need to compare old and new backing file */
-    if (unsafe) {
-        /* Make the compiler happy */
-        bs_old_backing = NULL;
-        bs_new_backing = NULL;
-    } else {
+    if (!unsafe) {
         char backing_name[1024];
 
         bs_old_backing = bdrv_new("old_backing", &error_abort);
Signature · 1 line
1.9.3
Reviewed-by on v1 3/3
#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] qemu-img: always goto out in img_snapshot() error paths

Quoted 12 lines · Stefan Hajnoczi

The out label has the qemu_progress_end() and other cleanup calls. Always goto out in error paths so the cleanup happens.

Note that bdrv_unref(NULL) is safe. We just need to initialize bs to NULL at the top of the function.

We can now remove the obsolete bs_old_backing = NULL and bs_new_backing = NULL for safe mode. Originally it was necessary in commit 3e85c6fd ("qemu-img rebase") but became useless in commit c2abcce ("qemu-img:
avoid calling exit(1) to release resources properly") because the variables are already initialized during declaration.

Quoted 5 lines
---
  qemu-img.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
#10 lore raw

Re: [Qemu-devel] [PATCH 3/3] qemu-img: always goto out in img_snapshot() error paths

In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

The out label has the qemu_progress_end() and other cleanup calls. Always goto out in error paths so the cleanup happens.

Note that bdrv_unref(NULL) is safe. We just need to initialize bs to NULL at the top of the function.

We can now remove the obsolete bs_old_backing = NULL and bs_new_backing = NULL for safe mode. Originally it was necessary in commit 3e85c6fd ("qemu-img rebase") but became useless in commit c2abcce ("qemu-img:
avoid calling exit(1) to release resources properly") because the variables are already initialized during declaration.

Please mention here that you fix exit code.from -1 to 1.

Quoted 3 lines

[...]

#11 lore raw

Re: [Qemu-devel] [PATCH 3/3] qemu-img: always goto out in img_snapshot() error paths

Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Stefan Hajnoczi

The out label has the qemu_progress_end() and other cleanup calls. Always goto out in error paths so the cleanup happens.

Note that bdrv_unref(NULL) is safe. We just need to initialize bs to NULL at the top of the function.

Quoted 7 lines
Quoted 5 lines

We can now remove the obsolete bs_old_backing = NULL and bs_new_backing = NULL for safe mode. Originally it was necessary in commit 3e85c6fd ("qemu-img rebase") but became useless in commit c2abcce ("qemu-img:
avoid calling exit(1) to release resources properly") because the variables are already initialized during declaration.

Please mention here that you fix exit code.from -1 to 1.

Fixed up when merging

Reviewed-by on the v1 series
#12 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

I noticed a few minor issues when looking at cache flag parsing. This series cleans them up.

Stefan Hajnoczi (3):
qemu-img: fix img_commit() error return value
qemu-img: fix img_compare() flags error path
qemu-img: always goto out in img_snapshot() error paths

  qemu-img.c | 23 ++++++++++-------------
  1 file changed, 10 insertions(+), 13 deletions(-)
Signature · 1 line
—js
#13 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi

I noticed a few minor issues when looking at cache flag parsing. This series cleans them up.

Stefan Hajnoczi (3):
qemu-img: fix img_commit() error return value
qemu-img: fix img_compare() flags error path
qemu-img: always goto out in img_snapshot() error paths

 qemu-img.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

Applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan