Skip to contentSkip to conversation

qcow2: Small error path fixes for snapshot writing

PATCH Applied Max Reitz 2 participants 5 messages Oct 9, 2013 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

cover qcow2: Small error path fixes for snapshot writing

Errors in qcow2_write_snapshots should always go down the error path. If this path is taken, the newly allocated snapshot table clusters are abandoned and should thus be freed.

Furthermore, we should safeguard against a possible future increase of QEMU's maximum snapshot name/ID length.

Max Reitz (3):
qcow2: Always use error path on writing snapshots
qcow2: Free allocated snapshot table on error
qcow2: Assert against snapshot name/ID overflow

 block/qcow2-snapshot.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 1/3 qcow2: Always use error path on writing snapshots

qcow2_write_snapshots does contain a fail label and there is no reason not to use it on some errors; therefore, we should always jump there on error.

---
 block/qcow2-snapshot.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
block/qcow2-snapshot.c +4 -3
index 5e8a779..3337974 100644
@@ -182,11 +182,12 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
     snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
     offset = snapshots_offset;
     if (offset < 0) {
-        return offset;
+        ret = offset;
+        goto fail;
     }
     ret = bdrv_flush(bs);
     if (ret < 0) {
-        return ret;
+        goto fail;
     }
 
     /* The snapshot list position has not yet been updated, so these clusters
@@ -194,7 +195,7 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
     ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_DEFAULT, offset,
                                         snapshots_size);
     if (ret < 0) {
-        return ret;
+        goto fail;
     }
 
 
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH 2/3 qcow2: Free allocated snapshot table on error

In reply to Max Reitz

If an error occurs during qcow2_write_snapshots, the newly allocated snapshot table clusters are leaked and should thus be freed.

---
 block/qcow2-snapshot.c | 4 ++++
 1 file changed, 4 insertions(+)
block/qcow2-snapshot.c +4 -0
index 3337974..f6f3e64 100644
@@ -279,6 +279,10 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
     return 0;
 
 fail:
+    if (snapshots_offset > 0) {
+        qcow2_free_clusters(bs, snapshots_offset, snapshots_size,
+                            QCOW2_DISCARD_ALWAYS);
+    }
     return ret;
 }
 
Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH 3/3 qcow2: Assert against snapshot name/ID overflow

In reply to Max Reitz

qcow2_write_snapshots relies on the length of every snapshot ID and name fitting into an unsigned 16 bit integer. This is currently ensured by QEMU through generally only allowing 128 byte IDs and 256 byte names. However, if this should change in the future, the length written to the image file should not be silently truncated (though the name itself would be written completely).

Since this is currently not an issue but might require attention due to internal QEMU changes in the future, an assert ensuring sanity is enough for now.

---
 block/qcow2-snapshot.c | 1 +
 1 file changed, 1 insertion(+)
block/qcow2-snapshot.c +1 -0
index f6f3e64..812dab2 100644
@@ -221,6 +221,7 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
 
         id_str_size = strlen(sn->id_str);
         name_size = strlen(sn->name);
+        assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX);
         h.id_str_size = cpu_to_be16(id_str_size);
         h.name_size = cpu_to_be16(name_size);
         offset = align_offset(offset, 8);
Signature · 1 line
1.8.3.1
Applied
#4 lore raw
In reply to Max Reitz

Am 09.10.2013 um 10:51 hat Max Reitz geschrieben:

Quoted 6 lines

Errors in qcow2_write_snapshots should always go down the error path. If this path is taken, the newly allocated snapshot table clusters are abandoned and should thus be freed.

Furthermore, we should safeguard against a possible future increase of QEMU's maximum snapshot name/ID length.

Thanks, applied all to the block branch.

Kevin