Skip to contentSkip to conversation

clvmd-cman.c: another unchecked malloc

PATCH Jim Meyering 1 participant 2 messages Apr 26, 2007 – Apr 27, 2007

lore
Share & tools
lore
#0 lore raw

There's an unchecked malloc in clvmd-cman.c's get_members function:

	if (node_updown == NULL) {
		node_updown =
			(int *) malloc(sizeof(int) *
				       max(num_nodes, max_updown_nodes));
		memset(node_updown, 0,
		       sizeof(int) * max(num_nodes, max_updown_nodes));
	}

Here's a fix.
At the same time, I've taken the liberty of factoring out the common sub-expression (buffer length) and eliminating the cast on malloc's return value.

* daemons/clvmd/clvmd-cman.c (get_members): Don't dereference NULL upon failed malloc.

Index: daemons/clvmd/clvmd-cman.c
===================================================================
RCS file: /cvs/lvm2/LVM2/daemons/clvmd/clvmd-cman.c,v retrieving revision 1.17

clvmd/clvmd-cman.c +4 -5
diff -u -p -r1.17 clvmd-cman.c
@@ -320,11 +320,10 @@ static void get_members()
 	}
 
 	if (node_updown == NULL) {
-		node_updown =
-			(int *) malloc(sizeof(int) *
-				       max(num_nodes, max_updown_nodes));
-		memset(node_updown, 0,
-		       sizeof(int) * max(num_nodes, max_updown_nodes));
+		size_t buf_len = sizeof(int) * max(num_nodes, max_updown_nodes));
+		node_updown = malloc(buf_len);
+		if (node_updown)
+			memset(node_updown, 0, buf_len);
 	}
 }
 
#1 lore raw
Quoted 26 lines · Jim Meyering

There's an unchecked malloc in clvmd-cman.c's get_members function:

	if (node_updown == NULL) {
		node_updown =
			(int *) malloc(sizeof(int) *
				       max(num_nodes, max_updown_nodes));
		memset(node_updown, 0,
		       sizeof(int) * max(num_nodes, max_updown_nodes));
	}

Here's a fix.
At the same time, I've taken the liberty of factoring out the common sub-expression (buffer length) and eliminating the cast on malloc's return value.

* daemons/clvmd/clvmd-cman.c (get_members): Don't dereference NULL upon failed malloc.

Index: daemons/clvmd/clvmd-cman.c
===================================================================
RCS file: /cvs/lvm2/LVM2/daemons/clvmd/clvmd-cman.c,v retrieving revision 1.17
diff -u -p -r1.17 clvmd-cman.c

clvmd/clvmd-cman.c static void get_members() · line 323

Quoted 9 lines
clvmd/clvmd-cman.c +1 -5
@@ -320,11 +320,10 @@ static void get_members()
 	}
 
 	if (node_updown == NULL) {
-		node_updown =
-			(int *) malloc(sizeof(int) *
-				       max(num_nodes, max_updown_nodes));
-		memset(node_updown, 0,
-		       sizeof(int) * max(num_nodes, max_updown_nodes));
+		size_t buf_len = sizeof(int) * max(num_nodes, max_updown_nodes));

Whoops.
Now that I've installed enough -devel packages to compile that file, I see that there are too many parentheses above.
Here's a corrected patch:

Index: daemons/clvmd/clvmd-cman.c
===================================================================
RCS file: /cvs/lvm2/LVM2/daemons/clvmd/clvmd-cman.c,v retrieving revision 1.17

clvmd/clvmd-cman.c +4 -5
diff -u -p -r1.17 clvmd-cman.c
@@ -320,11 +320,10 @@ static void get_members()
 	}
 
 	if (node_updown == NULL) {
-		node_updown =
-			(int *) malloc(sizeof(int) *
-				       max(num_nodes, max_updown_nodes));
-		memset(node_updown, 0,
-		       sizeof(int) * max(num_nodes, max_updown_nodes));
+		size_t bufsiz = sizeof(int) * max(num_nodes, max_updown_nodes);
+		node_updown = malloc(bufsiz);
+		if (node_updown)
+			memset(node_updown, 0, bufsiz);
 	}
 }