clvmd-cman.c: another unchecked malloc
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
@@ -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); } }
Quoted 26 lines · Jim Meyering
There's an unchecked malloc in clvmd-cman.c's
get_membersfunction: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.17clvmd-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
@@ -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); } }