From: Lee Schermerhorn <lee.schermerhorn@hp.com>
To: Cliff Wickman <cpi@sgi.com>
Cc: Kornilios Kourtis <kkourt@cslab.ece.ntua.gr>,
	Brice Goglin <Brice.Goglin@inria.fr>, <linux-numa@vger.kernel.org>,
	<eric.whitney@hp.com>
Date:	Tue, 28 Apr 2009 12:36:32 -0400
Subject: [PATCH 2/8] numactl/libnuma - Simple bitmask leak fixes

[PATCH 02/08] Fix Simple Bitmask Leaks

Against:  numactl-2.0.3-rc2

Add or move numa_bitmask_free() or exit via path that already
frees bitmask to plug leaks.

Kornilios pulled a couple of these hunks out of a single big patch
that I had originally created.  I figured the node_parse_*string()
hunks were simple enough to fit here as well.

Note:  this patch leaves a bitmask leak in numa_parse_nodestring()
that will be fixed in a later patch.

 libnuma.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Index: numactl-dev/libnuma.c
===================================================================
--- numactl-dev.orig/libnuma.c
+++ numactl-dev/libnuma.c
@@ -763,6 +763,7 @@ void numa_tonode_memory(void *mem, size_
 	nodes = numa_allocate_nodemask();
 	numa_bitmask_setbit(nodes, node);
 	dombind(mem, size, bind_policy, nodes);
+	numa_bitmask_free(nodes);
 }
 
 void
@@ -890,6 +891,7 @@ numa_get_interleave_mask_v1(void)
 		copy_bitmask_to_nodemask(bmp, &mask);
 	else
 	 	copy_bitmask_to_nodemask(numa_no_nodes_ptr, &mask);
+	numa_bitmask_free(bmp);
 	return mask;
 }
 __asm__(".symver numa_get_interleave_mask_v1,numa_get_interleave_mask@libnuma_1.1");
@@ -1423,13 +1425,13 @@ numa_run_on_node_mask_v2(struct bitmask
 	}
 	err = numa_sched_setaffinity_v2_int(0, cpus);
 
+	numa_bitmask_free(cpus);
+	numa_bitmask_free(nodecpus);
+
 	/* used to have to consider that this could fail - it shouldn't now */
 	if (err < 0) {
 		numa_error("numa_sched_setaffinity_v2_int() failed; abort\n");
-		return -1;
 	}
-	numa_bitmask_free(cpus);
-	numa_bitmask_free(nodecpus);
 
 	return err;
 } 
@@ -1700,7 +1702,7 @@ numa_parse_nodestring(char *s)
 		}
 	} while (*s++ == ',');
 	if (s[-1] != '\0')
-		return 0;
+		goto err;
 	if (invert) {
 		int i;
 		for (i = 0; i < conf_nodes; i++) {
@@ -1796,7 +1798,7 @@ numa_parse_cpustring(char *s)
 		}
 	} while (*s++ == ',');
 	if (s[-1] != '\0')
-		return 0;
+		goto err;
 	if (invert) {
 		int i;
 		for (i = 0; i < conf_cpus; i++) {
