1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

virStorageBackendRBDGetVolNames: Fix memory leak in 'rbd_list2' version

The 'rbd_image_spec_t' struct has two string members 'id' and
'name'. We only stole the 'name' members thus the 'id's as well as the
whole list would be leaked on success.

Restructure the code so that we copy out the image names and call
rbd_image_spec_list_cleanup on success rather than on error.

The error path is then handled by using g_autofree for 'images'.

Since we no longer have a error path after allocating the returned
string list we can completely remove its cleanup.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-06-14 17:14:13 +02:00
parent 7d50abb805
commit 361a18f405

View File

@ -579,9 +579,8 @@ static char **
virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr)
{
char **names = NULL;
size_t nnames = 0;
int rc;
rbd_image_spec_t *images = NULL;
g_autofree rbd_image_spec_t *images = NULL;
size_t nimages = 16;
size_t i;
@ -593,23 +592,18 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr)
break;
if (rc != -ERANGE) {
virReportSystemError(errno, "%s", _("Unable to list RBD images"));
goto error;
return NULL;
}
}
names = g_new0(char *, nimages + 1);
nnames = nimages;
for (i = 0; i < nimages; i++)
names[i] = g_steal_pointer(&images[i].name);
names[i] = g_strdup(images[i].name);
rbd_image_spec_list_cleanup(images, nimages);
return names;
error:
virStringListFreeCount(names, nnames);
rbd_image_spec_list_cleanup(images, nimages);
VIR_FREE(images);
return NULL;
}
#else /* ! WITH_RBD_LIST2 */