libkvm - kvm_get_swapinfo - fix memory leak in last commit
authorMatthew Dillon <dillon@apollo.backplane.com>
Wed, 18 Aug 2010 00:52:59 +0000 (17:52 -0700)
committerMatthew Dillon <dillon@apollo.backplane.com>
Wed, 18 Aug 2010 00:52:59 +0000 (17:52 -0700)
* Fix a memory leak

lib/libkvm/kvm_getswapinfo.c

index 3e7915d..b2558d1 100644 (file)
@@ -495,11 +495,16 @@ kvm_getswapinfo_sysctl(kvm_t *kd, struct kvm_swap *swap_ary,
                return(-1);
        if (bytes == 0)
                return(-1);
+
        xswbuf = malloc(bytes);
-       if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0)
+       if (sysctlbyname("vm.swap_info_array", xswbuf, &bytes, NULL, 0) < 0) {
+               free(xswbuf);
                return(-1);
-       if (bytes == 0)
+       }
+       if (bytes == 0) {
+               free(xswbuf);
                return(-1);
+       }
 
        bzero(swap_ary, sizeof(struct kvm_swap) * swap_max);
        --swap_max;
@@ -541,5 +546,6 @@ kvm_getswapinfo_sysctl(kvm_t *kd, struct kvm_swap *swap_ary,
                }
                ++ti;
        }
+       free(xswbuf);
        return(ti);
 }