Skip to contentSkip to conversation

ibacm: Check dynamic memory allocation results

PATCH In review Dotan Barak 2 participants 2 messages Nov 28, 2011 1 message not in the archive InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ibacm: Check dynamic memory allocation results

After allocation of dynamic memory blocks, check that the allocation succeeded, if it failed handle it gracefully.

---
 src/parse.c |   22 ++++++++++++++++------
 1 files changed, 16 insertions(+), 6 deletions(-)
src/parse.c +16 -6
index d1c820f..6ab6041 100644
@@ -71,14 +71,15 @@ static char *expand(char *basename, char *args, int *str_cnt, int *str_size)
 
 char **parse(char *args, int *count)
 {
-	char **ptrs;
+	char **ptrs = NULL;
 	char *str_buf, *cpy, *token, *next;
 	int cnt = 0, str_size = 0;
 	int i;
 
 	/* make a copy that strtok can modify */
-	cpy = malloc(strlen(args) + 1);
-	strcpy(cpy, args);
+	cpy = strdup(args);
+	if (!cpy)
+		return NULL;
 
 	if (args[0] == '[') {
 		cpy[0] = '\0';
@@ -92,6 +93,8 @@ char **parse(char *args, int *count)
 	if (!next) {
 		str_size = strlen(token) + 1;
 		str_buf = malloc(str_size);
+		if (!str_buf)
+			goto out_cpy;
 		strcpy(str_buf, token);
 		cnt = 1;
 	} else {
@@ -99,6 +102,9 @@ char **parse(char *args, int *count)
 	}
 
 	ptrs = malloc((sizeof str_buf * (cnt + 1)) + str_size);
+	if (!ptrs)
+		goto out_str_buf;
+
 	memcpy(&ptrs[cnt + 1], str_buf, str_size);
 
 	ptrs[0] = (char*) &ptrs[cnt + 1];
@@ -106,10 +112,14 @@ char **parse(char *args, int *count)
 		ptrs[i] = index(ptrs[i - 1], 0) + 1;
 	ptrs[i] = NULL;
 
-	free(cpy);
-	free(str_buf);
-
 	if (count)
 		*count = cnt;
+
+out_str_buf:
+        free(str_buf);
+
+out_cpy:
+	free(cpy);
+
 	return ptrs;
 }
Signature · 6 lines
1.7.4.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 201111282135.11283.dotanb-g2bIKuvJtcQanlnWow0HJg@public.gmane.org