* [LTP] [PATCH]: Add check for realloc failure case
@ 2015-05-28 4:30 Manjeet Pawar
2015-06-01 14:22 ` Cyril Hrubis
0 siblings, 1 reply; 6+ messages in thread
From: Manjeet Pawar @ 2015-05-28 4:30 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net; +Cc: ajeet.y@samsung.com, Yogesh Narayan Gaur
From: Manjeet Pawar <manjeet.p@samsung.com>
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
Signed-off-by: Yogesh Gaur <yn.gaur@samsung.com>
Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
---
pan/splitstr.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/pan/splitstr.c b/pan/splitstr.c
index 4706323..8804b15 100644
--- a/pan/splitstr.c
+++ b/pan/splitstr.c
@@ -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;
--
1.7.9.5
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [LTP] [PATCH]: Add check for realloc failure case
2015-05-28 4:30 [LTP] [PATCH]: Add check for realloc failure case Manjeet Pawar
@ 2015-06-01 14:22 ` Cyril Hrubis
0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2015-06-01 14:22 UTC (permalink / raw)
To: Manjeet Pawar
Cc: ajeet.y@samsung.com, ltp-list@lists.sourceforge.net,
Yogesh Narayan Gaur
Hi!
> @@ -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);
> + }
> +
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.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 6+ messages in thread
* [LTP] [PATCH]: Add check for realloc() failure case
@ 2015-05-28 4:33 Manjeet Pawar
2015-06-01 14:25 ` Cyril Hrubis
0 siblings, 1 reply; 6+ messages in thread
From: Manjeet Pawar @ 2015-05-28 4:33 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net; +Cc: ajeet.y@samsung.com, Yogesh Narayan Gaur
From: Manjeet Pawar <manjeet.p@samsung.com>
Subject: [PATCH] testcases/kernel/fs/fsstress/fsstress.c: Add check for realloc() failure case
This patch add check for realloc failure case and exit from the testcase if realloc fails.
Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
Signed-off-by: Yogesh Gaur <yn.gaur@samsung.com>
---
testcases/kernel/fs/fsstress/fsstress.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/testcases/kernel/fs/fsstress/fsstress.c b/testcases/kernel/fs/fsstress/fsstress.c
index 2243280..ce74f99 100644
--- a/testcases/kernel/fs/fsstress/fsstress.c
+++ b/testcases/kernel/fs/fsstress/fsstress.c
@@ -330,8 +330,12 @@ int main(int argc, char **argv)
process_freq(optarg);
break;
case 'i':
- ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
- ilist[ilistlen - 1] = strtol(optarg, &p, 16);
+ if(realloc(ilist, ++ilistlen * sizeof(*ilist)) != NULL)
+ ilist[ilistlen - 1] = strtol(optarg, &p, 16);
+ else{
+ fprintf(stderr, "realloc: New memory allocation failed \n");
+ exit(1);
+ }
break;
case 'l':
loops = atoi(optarg);
--
1.7.9.5
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [LTP] [PATCH]: Add check for realloc() failure case
2015-05-28 4:33 [LTP] [PATCH]: Add check for realloc() " Manjeet Pawar
@ 2015-06-01 14:25 ` Cyril Hrubis
0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2015-06-01 14:25 UTC (permalink / raw)
To: Manjeet Pawar
Cc: ajeet.y@samsung.com, ltp-list@lists.sourceforge.net,
Yogesh Narayan Gaur
Hi!
> case 'i':
> - ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
> - ilist[ilistlen - 1] = strtol(optarg, &p, 16);
> + if(realloc(ilist, ++ilistlen * sizeof(*ilist)) != NULL)
> + ilist[ilistlen - 1] = strtol(optarg, &p, 16);
This code is wrong. The realloc() may return _different_ address (and
will copy the data if it does so).
> + else{
> + fprintf(stderr, "realloc: New memory allocation failed \n");
> + exit(1);
> + }
> break;
> case 'l':
> loops = atoi(optarg);
Also the coding style does not follow LKML.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 6+ messages in thread
* [LTP] [PATCH]: Add check for realloc() failure case
@ 2015-05-28 4:36 Manjeet Pawar
0 siblings, 0 replies; 6+ messages in thread
From: Manjeet Pawar @ 2015-05-28 4:36 UTC (permalink / raw)
To: ltp-list@lists.sourceforge.net; +Cc: ajeet.y@samsung.com, Yogesh Narayan Gaur
From: Manjeet Pawar <manjeet.p@samsung.com>
Subject: [PATCH] testcases/network/nfs/nfs_fsstress/fsstress.c: Add check for realloc() failure case
This patch add check for realloc failure case and exit from the testcase if realloc fails.
Signed-off-by: Yogesh Gaur <yn.gaur@samsung.com>
Signed-off-by: Manjeet Pawar <manjeet.p@samsung.com>
---
testcases/network/nfs/nfs_fsstress/fsstress.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/testcases/network/nfs/nfs_fsstress/fsstress.c b/testcases/network/nfs/nfs_fsstress/fsstress.c
index ac91262..208c2dc 100644
--- a/testcases/network/nfs/nfs_fsstress/fsstress.c
+++ b/testcases/network/nfs/nfs_fsstress/fsstress.c
@@ -330,8 +330,12 @@ int main(int argc, char **argv)
process_freq(optarg);
break;
case 'i':
- ilist = realloc(ilist, ++ilistlen * sizeof(*ilist));
- ilist[ilistlen - 1] = strtol(optarg, &p, 16);
+ if(realloc(ilist, ++ilistlen * sizeof(*ilist)) != NULL)
+ ilist[ilistlen - 1] = strtol(optarg, &p, 16);
+ else{
+ fprintf(stderr, "realloc: New memory allocation failed \n");
+ exit(1);
+ }
break;
case 'l':
loops = atoi(optarg);
--
1.7.9.5
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply related [flat|nested] 6+ messages in thread[parent not found: <149090115.398111433218346536.JavaMail.weblogic@epmlwas08b>]
* Re: [LTP] [PATCH]: Add check for realloc failure case
[not found] <149090115.398111433218346536.JavaMail.weblogic@epmlwas08b>
@ 2015-06-04 16:27 ` Cyril Hrubis
0 siblings, 0 replies; 6+ messages in thread
From: Cyril Hrubis @ 2015-06-04 16:27 UTC (permalink / raw)
To: Manjeet Pawar
Cc: AJEET YADAV, ltp-list@lists.sourceforge.net, Yogesh Narayan Gaur
Hi!
> @@ -113,6 +114,12 @@ const char **splitstr(const char *str, const char *separator, int *argcount)
> arg_array =
> (char **)realloc((void *)arg_array,
> sizeof(char *) * max_toks);
> + if (arg_array == NULL) {
> + fprintf(stderr, "realloc: New memory allocation failed \n");
> + free(arg_array);
I've removed the free(arg_array) here, because it's noop (doing free(NULL)) and pushed both patches.
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2015-06-04 16:28 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-28 4:30 [LTP] [PATCH]: Add check for realloc failure case Manjeet Pawar
2015-06-01 14:22 ` Cyril Hrubis
-- strict thread matches above, loose matches on Subject: below --
2015-05-28 4:33 [LTP] [PATCH]: Add check for realloc() " Manjeet Pawar
2015-06-01 14:25 ` Cyril Hrubis
2015-05-28 4:36 Manjeet Pawar
[not found] <149090115.398111433218346536.JavaMail.weblogic@epmlwas08b>
2015-06-04 16:27 ` [LTP] [PATCH]: Add check for realloc " Cyril Hrubis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox