[libvirt] [PATCH] LXC: fix memory leak in lxcContainerGetSubtree

when libvirt_lxc trigger oom error in lxcContainerGetSubtree we should free the alloced memory for mounts. so when lxcContainerGetSubtree failed,we should do some memory cleanup in lxcContainerUnmountSubtree. Signed-off-by: Gao feng <gaofeng@cn.fujitsu.com> --- src/lxc/lxc_container.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 5651255..633218c 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -1118,10 +1118,11 @@ static int lxcContainerGetSubtree(const char *prefix, virReportOOMError(); goto cleanup; } - if (!(mounts[nmounts++] = strdup(mntent.mnt_dir))) { + if (!(mounts[nmounts] = strdup(mntent.mnt_dir))) { virReportOOMError(); goto cleanup; } + nmounts++; VIR_DEBUG("Grabbed %s", mntent.mnt_dir); } @@ -1129,11 +1130,10 @@ static int lxcContainerGetSubtree(const char *prefix, qsort(mounts, nmounts, sizeof(mounts[0]), lxcContainerChildMountSort); - *mountsret = mounts; - *nmountsret = nmounts; ret = 0; - cleanup: + *mountsret = mounts; + *nmountsret = nmounts; endmntent(procmnt); return ret; } @@ -1151,7 +1151,7 @@ static int lxcContainerUnmountSubtree(const char *prefix, VIR_DEBUG("Unmount subtreee from %s", prefix); if (lxcContainerGetSubtree(prefix, &mounts, &nmounts) < 0) - return -1; + goto cleanup; for (i = 0 ; i < nmounts ; i++) { VIR_DEBUG("Umount %s", mounts[i]); if (umount(mounts[i]) < 0) { -- 1.7.7.6

On 2012年06月12日 14:31, Gao feng wrote:
when libvirt_lxc trigger oom error in lxcContainerGetSubtree we should free the alloced memory for mounts.
so when lxcContainerGetSubtree failed,we should do some memory cleanup in lxcContainerUnmountSubtree.
Signed-off-by: Gao feng<gaofeng@cn.fujitsu.com> --- src/lxc/lxc_container.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 5651255..633218c 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -1118,10 +1118,11 @@ static int lxcContainerGetSubtree(const char *prefix, virReportOOMError(); goto cleanup; } - if (!(mounts[nmounts++] = strdup(mntent.mnt_dir))) { + if (!(mounts[nmounts] = strdup(mntent.mnt_dir))) {
Good catch.
virReportOOMError(); goto cleanup; } + nmounts++; VIR_DEBUG("Grabbed %s", mntent.mnt_dir); }
@@ -1129,11 +1130,10 @@ static int lxcContainerGetSubtree(const char *prefix, qsort(mounts, nmounts, sizeof(mounts[0]), lxcContainerChildMountSort);
- *mountsret = mounts; - *nmountsret = nmounts; ret = 0; - cleanup: + *mountsret = mounts; + *nmountsret = nmounts; endmntent(procmnt); return ret; } @@ -1151,7 +1151,7 @@ static int lxcContainerUnmountSubtree(const char *prefix, VIR_DEBUG("Unmount subtreee from %s", prefix);
if (lxcContainerGetSubtree(prefix,&mounts,&nmounts)< 0) - return -1; + goto cleanup; for (i = 0 ; i< nmounts ; i++) { VIR_DEBUG("Umount %s", mounts[i]); if (umount(mounts[i])< 0) {
Right fix, and ACK. Osier

On 2012年06月12日 15:01, Osier Yang wrote:
On 2012年06月12日 14:31, Gao feng wrote:
when libvirt_lxc trigger oom error in lxcContainerGetSubtree we should free the alloced memory for mounts.
so when lxcContainerGetSubtree failed,we should do some memory cleanup in lxcContainerUnmountSubtree.
Signed-off-by: Gao feng<gaofeng@cn.fujitsu.com> --- src/lxc/lxc_container.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 5651255..633218c 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -1118,10 +1118,11 @@ static int lxcContainerGetSubtree(const char *prefix, virReportOOMError(); goto cleanup; } - if (!(mounts[nmounts++] = strdup(mntent.mnt_dir))) { + if (!(mounts[nmounts] = strdup(mntent.mnt_dir))) {
Good catch.
virReportOOMError(); goto cleanup; } + nmounts++; VIR_DEBUG("Grabbed %s", mntent.mnt_dir); }
@@ -1129,11 +1130,10 @@ static int lxcContainerGetSubtree(const char *prefix, qsort(mounts, nmounts, sizeof(mounts[0]), lxcContainerChildMountSort);
- *mountsret = mounts; - *nmountsret = nmounts; ret = 0; - cleanup: + *mountsret = mounts; + *nmountsret = nmounts; endmntent(procmnt); return ret; } @@ -1151,7 +1151,7 @@ static int lxcContainerUnmountSubtree(const char *prefix, VIR_DEBUG("Unmount subtreee from %s", prefix);
if (lxcContainerGetSubtree(prefix,&mounts,&nmounts)< 0) - return -1; + goto cleanup; for (i = 0 ; i< nmounts ; i++) { VIR_DEBUG("Umount %s", mounts[i]); if (umount(mounts[i])< 0) {
Right fix, and ACK.
Osier
Pushed.

On 06/12/2012 12:31 AM, Gao feng wrote:
when libvirt_lxc trigger oom error in lxcContainerGetSubtree we should free the alloced memory for mounts.
so when lxcContainerGetSubtree failed,we should do some memory cleanup in lxcContainerUnmountSubtree.
virReportOOMError(); goto cleanup; } + nmounts++; VIR_DEBUG("Grabbed %s", mntent.mnt_dir);
Whitespace damage. We don't use TAB's, and 'make syntax-check' would have caught this. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

于 2012年06月19日 00:13, Eric Blake 写道:
On 06/12/2012 12:31 AM, Gao feng wrote:
when libvirt_lxc trigger oom error in lxcContainerGetSubtree we should free the alloced memory for mounts.
so when lxcContainerGetSubtree failed,we should do some memory cleanup in lxcContainerUnmountSubtree.
virReportOOMError(); goto cleanup; } + nmounts++; VIR_DEBUG("Grabbed %s", mntent.mnt_dir);
Whitespace damage. We don't use TAB's, and 'make syntax-check' would have caught this.
thanks,I will pay attention to it.
participants (3)
-
Eric Blake
-
Gao feng
-
Osier Yang