Skip to contentSkip to conversation

: Add check for realloc failure case

PATCH In review Manjeet Pawar 2 participants 2 messages May 28, 2015 – Jun 1, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : Add check for realloc failure case

From: Manjeet Pawar <[email protected]>
Subject: [PATCH] pan/splitstr.c: Add check for realloc failure case

This patch adds check for realloc failure case and free arg_string before returning from splitstr() to avoid memory leaks

---
 pan/splitstr.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
pan/splitstr.c +8 -3
index 4706323..8804b15 100644
@@ -93,6 +93,7 @@ const char **splitstr(const char *str, const char *separator, int *argcount)
 	if (arg_array == NULL) {
 		if (argcount != NULL)
 			*argcount = 0;
+		free(arg_string);
 		return (NULL);
 	}
 
@@ -110,9 +111,13 @@ const char **splitstr(const char *str, const char *separator, int *argcount)
 		cur_tok = strtok(NULL, separator);
 		if (num_toks == max_toks) {
 			max_toks += 20;
-			arg_array =
-			    (char **)realloc((void *)arg_array,
-					     sizeof(char *) * max_toks);
+			if((arg_array = realloc((void *)arg_array,sizeof(char *) * max_toks)) == NULL){
+				fprintf(stderr, "realloc: New memory allocation failed \n");
+				free(arg_array);
+				free(arg_string);
+				exit(1);
+			}
+
 		}
 	}
 	arg_array[num_toks] = NULL;
Signature · 6 lines
1.7.9.5
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw

Hi!

Quoted 4 lines
pan/splitstr.c +0 -0
@@ -110,9 +111,13 @@ const char **splitstr(const char *str, const char *separator, int *argcount)
 		cur_tok = strtok(NULL, separator);
 		if (num_toks == max_toks) {
 			max_toks += 20;

pan/splitstr.c const char **splitstr(const char *str, const char *separator, int *argcount) · line 120

Quoted 10 lines
pan/splitstr.c +7 -3
-			arg_array =
-			    (char **)realloc((void *)arg_array,
-					     sizeof(char *) * max_toks);
+			if((arg_array = realloc((void *)arg_array,sizeof(char *) * max_toks)) == NULL){
+				fprintf(stderr, "realloc: New memory allocation failed \n");
+				free(arg_array);
+				free(arg_string);
+				exit(1);
+			}
+

These lines does not conform to LKML coding style.

There should be space before the opening curly brace '{', spaces after coma, etc.

You can use checkpatch.pl shipped with linux kernel source to check your patches before sending them.

Signature · 8 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list