[libvirt] [PATCH] ESX: Fix memory leak in list handling functions.

If an error occurs between the allocation of an item and appending it to the list, the item leaks. Free such orphaned items in error cases. * src/esx/esx_vi.c: free orphaned items in error cases --- src/esx/esx_vi.c | 24 ++++++++---------------- 1 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index bcf110f..04860e2 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -959,28 +959,22 @@ esxVI_List_CastFromAnyType(virConnectPtr conn, esxVI_AnyType *anyType, esxVI_AnyType_Free(&childAnyType); - if (esxVI_AnyType_Deserialize(conn, childNode, &childAnyType) < 0) { + if (esxVI_AnyType_Deserialize(conn, childNode, &childAnyType) < 0 || + castFromAnyTypeFunc(conn, childAnyType, &item) < 0 || + esxVI_List_Append(conn, list, item) < 0) { goto failure; } item = NULL; - - if (castFromAnyTypeFunc(conn, childAnyType, &item) < 0) { - goto failure; - } - - if (esxVI_List_Append(conn, list, item) < 0) { - goto failure; - } } - cleanup: esxVI_AnyType_Free(&childAnyType); return result; failure: + freeFunc(&item); freeFunc(list); result = -1; @@ -1039,20 +1033,18 @@ esxVI_List_Deserialize(virConnectPtr conn, xmlNodePtr node, esxVI_List **list, goto failure; } - item = NULL; - - if (deserializeFunc(conn, node, &item) < 0) { + if (deserializeFunc(conn, node, &item) < 0 || + esxVI_List_Append(conn, list, item) < 0) { goto failure; } - if (esxVI_List_Append(conn, list, item) < 0) { - goto failure; - } + item = NULL; } return 0; failure: + freeFunc(&item); freeFunc(list); return -1; -- 1.6.0.4

On Wed, Oct 28, 2009 at 09:12:57PM +0100, Matthias Bolte wrote:
If an error occurs between the allocation of an item and appending it to the list, the item leaks. Free such orphaned items in error cases.
* src/esx/esx_vi.c: free orphaned items in error cases --- src/esx/esx_vi.c | 24 ++++++++---------------- 1 files changed, 8 insertions(+), 16 deletions(-)
diff --git a/src/esx/esx_vi.c b/src/esx/esx_vi.c index bcf110f..04860e2 100644 --- a/src/esx/esx_vi.c +++ b/src/esx/esx_vi.c @@ -959,28 +959,22 @@ esxVI_List_CastFromAnyType(virConnectPtr conn, esxVI_AnyType *anyType,
esxVI_AnyType_Free(&childAnyType);
- if (esxVI_AnyType_Deserialize(conn, childNode, &childAnyType) < 0) { + if (esxVI_AnyType_Deserialize(conn, childNode, &childAnyType) < 0 || + castFromAnyTypeFunc(conn, childAnyType, &item) < 0 || + esxVI_List_Append(conn, list, item) < 0) { goto failure; }
item = NULL; - - if (castFromAnyTypeFunc(conn, childAnyType, &item) < 0) { - goto failure; - } - - if (esxVI_List_Append(conn, list, item) < 0) { - goto failure; - } }
- cleanup: esxVI_AnyType_Free(&childAnyType);
return result;
failure: + freeFunc(&item); freeFunc(list);
result = -1; @@ -1039,20 +1033,18 @@ esxVI_List_Deserialize(virConnectPtr conn, xmlNodePtr node, esxVI_List **list, goto failure; }
- item = NULL; - - if (deserializeFunc(conn, node, &item) < 0) { + if (deserializeFunc(conn, node, &item) < 0 || + esxVI_List_Append(conn, list, item) < 0) { goto failure; }
- if (esxVI_List_Append(conn, list, item) < 0) { - goto failure; - } + item = NULL; }
return 0;
failure: + freeFunc(&item); freeFunc(list);
return -1;
ACK, looks fine Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
participants (2)
-
Daniel P. Berrange
-
Matthias Bolte