Skip to contentSkip to conversation

rbd: avoid qemu_rbd_snap_list() memory leak when no snapshots

PATCH Applied Stefan Hajnoczi 3 participants 4 messages Aug 14, 2013 – Sep 25, 2013 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rbd: avoid qemu_rbd_snap_list() memory leak when no snapshots

When there are no snapshots qemu_rbd_snap_list() returns 0 and the snapshot table pointer is NULL. Don't forget to free the snaps buffer we allocated for librbd rbd_snap_list().

---
 block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/rbd.c +1 -1
index cb71751..4e26fea 100644
@@ -934,7 +934,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,
     do {
         snaps = g_malloc(sizeof(*snaps) * max_snaps);
         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
-        if (snap_count < 0) {
+        if (snap_count <= 0) {
             g_free(snaps);
         }
     } while (snap_count == -ERANGE);
Signature · 1 line
1.8.3.1
#1 lore raw

Quoting Stefan Hajnoczi (2013-08-14 07:13:52)

Quoted 6 lines

When there are no snapshots qemu_rbd_snap_list() returns 0 and the snapshot table pointer is NULL. Don't forget to free the snaps buffer we allocated for librbd rbd_snap_list().

Ping for 1.6.1

Quoted 19 lines
---
 block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/rbd.c +1 -1
index cb71751..4e26fea 100644
@@ -934,7 +934,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,
     do {
         snaps = g_malloc(sizeof(*snaps) * max_snaps);
         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
-        if (snap_count < 0) {
+        if (snap_count <= 0) {
             g_free(snaps);
         }
     } while (snap_count == -ERANGE);
Signature · 1 line
1.8.3.1
Applied
#2 lore raw

Am 25.09.2013 um 02:28 hat Michael Roth geschrieben:

Quoted 9 lines

Quoting Stefan Hajnoczi (2013-08-14 07:13:52)

Quoted 6 lines

When there are no snapshots qemu_rbd_snap_list() returns 0 and the snapshot table pointer is NULL. Don't forget to free the snaps buffer we allocated for librbd rbd_snap_list().

Ping for 1.6.1

Applied it to the block branch for now, but...

Quoted 4 lines
Quoted 4 lines
---
 block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

block/rbd.c static int qemu_rbd_snap_list(BlockDriverState *bs, · line 940

Quoted 13 lines
block/rbd.c +1 -1
index cb71751..4e26fea 100644
@@ -934,7 +934,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,
     do {
         snaps = g_malloc(sizeof(*snaps) * max_snaps);
         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
-        if (snap_count < 0) {
+        if (snap_count <= 0) {
             g_free(snaps);
         }
     } while (snap_count == -ERANGE);

...I think this isn't a complete fix. In the successful case we still leak snaps. The g_free() should probably be moved to after the done:
label in a v2 of the patch.

Kevin

#3 lore raw
Quoted 26 lines · Kevin Wolf

Am 25.09.2013 um 02:28 hat Michael Roth geschrieben:

Quoted 9 lines

Quoting Stefan Hajnoczi (2013-08-14 07:13:52)

Quoted 6 lines

When there are no snapshots qemu_rbd_snap_list() returns 0 and the snapshot table pointer is NULL. Don't forget to free the snaps buffer we allocated for librbd rbd_snap_list().

Ping for 1.6.1

Applied it to the block branch for now, but...

Quoted 13 lines
Quoted 13 lines
---
 block/rbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/rbd.c +0 -1
index cb71751..4e26fea 100644
@@ -934,7 +934,7 @@ static int qemu_rbd_snap_list(BlockDriverState *bs,
     do {
         snaps = g_malloc(sizeof(*snaps) * max_snaps);
         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
-        if (snap_count < 0) {
Quoted 8 lines
Quoted 4 lines

block/rbd.c static int qemu_rbd_snap_list(BlockDriverState *bs, · line 940

Quoted 4 lines
block/rbd.c +1 -0
+        if (snap_count <= 0) {
             g_free(snaps);
         }
     } while (snap_count == -ERANGE);

...I think this isn't a complete fix. In the successful case we still leak snaps. The g_free() should probably be moved to after the done:
label in a v2 of the patch.

You are right. I'm sending a v2. rbd_snap_list_end() does not free snaps itself, only the strings that snaps[i].name points to. Therefore we need to free snaps.