* [PATCH] btrfs-progs: fix memory leaks in error path
@ 2015-08-21 3:10 Byongho Lee
2015-08-21 3:50 ` Zhao Lei
2015-08-25 15:36 ` David Sterba
0 siblings, 2 replies; 4+ messages in thread
From: Byongho Lee @ 2015-08-21 3:10 UTC (permalink / raw)
To: linux-btrfs
This patch includes below fixes in error path:
1. fix memory leaks if realloc() fails
2. add missing call free_history() before return error in scrub_read_file()
Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
---
btrfs-list.c | 8 ++++++++
cmds-scrub.c | 18 ++++++++++++++----
qgroup.c | 8 ++++++++
3 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/btrfs-list.c b/btrfs-list.c
index 875a89dc4ef0..d54de61aec01 100644
--- a/btrfs-list.c
+++ b/btrfs-list.c
@@ -254,11 +254,15 @@ static int btrfs_list_setup_comparer(struct btrfs_list_comparer_set **comp_set,
BUG_ON(set->ncomps > set->total);
if (set->ncomps == set->total) {
+ void *tmp;
+
size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
+ tmp = set;
set = realloc(set, size);
if (!set) {
fprintf(stderr, "memory allocation failed\n");
+ free(tmp);
exit(1);
}
@@ -1232,11 +1236,15 @@ int btrfs_list_setup_filter(struct btrfs_list_filter_set **filter_set,
BUG_ON(set->nfilters > set->total);
if (set->nfilters == set->total) {
+ void *tmp;
+
size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
+ tmp = set;
set = realloc(set, size);
if (!set) {
fprintf(stderr, "memory allocation failed\n");
+ free(tmp);
exit(1);
}
diff --git a/cmds-scrub.c b/cmds-scrub.c
index 5a85dc473c94..91cf67841849 100644
--- a/cmds-scrub.c
+++ b/cmds-scrub.c
@@ -502,12 +502,16 @@ again:
}
return p;
}
- if (avail == -1)
+ if (avail == -1) {
+ free_history(p);
return ERR_PTR(-errno);
+ }
avail += old_avail;
i = 0;
while (i < avail) {
+ void *tmp;
+
switch (state) {
case 0: /* start of file */
ret = scrub_kvread(&i,
@@ -534,11 +538,17 @@ again:
continue;
}
++curr;
+ tmp = p;
p = realloc(p, (curr + 2) * sizeof(*p));
- if (p)
- p[curr] = malloc(sizeof(**p));
- if (!p || !p[curr])
+ if (!p) {
+ free_history(tmp);
return ERR_PTR(-errno);
+ }
+ p[curr] = malloc(sizeof(**p));
+ if (!p[curr]) {
+ free_history(p);
+ return ERR_PTR(-errno);
+ }
memset(p[curr], 0, sizeof(**p));
p[curr + 1] = NULL;
++state;
diff --git a/qgroup.c b/qgroup.c
index dc04b033b145..327abd645f16 100644
--- a/qgroup.c
+++ b/qgroup.c
@@ -465,12 +465,16 @@ int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set **comp_set,
BUG_ON(set->ncomps > set->total);
if (set->ncomps == set->total) {
+ void *tmp;
+
size = set->total + BTRFS_QGROUP_NCOMPS_INCREASE;
size = sizeof(*set) +
size * sizeof(struct btrfs_qgroup_comparer);
+ tmp = set;
set = realloc(set, size);
if (!set) {
fprintf(stderr, "memory allocation failed\n");
+ free(tmp);
exit(1);
}
@@ -836,12 +840,16 @@ int btrfs_qgroup_setup_filter(struct btrfs_qgroup_filter_set **filter_set,
BUG_ON(set->nfilters > set->total);
if (set->nfilters == set->total) {
+ void *tmp;
+
size = set->total + BTRFS_QGROUP_NFILTERS_INCREASE;
size = sizeof(*set) + size * sizeof(struct btrfs_qgroup_filter);
+ tmp = set;
set = realloc(set, size);
if (!set) {
fprintf(stderr, "memory allocation failed\n");
+ free(tmp);
exit(1);
}
memset(&set->filters[set->total], 0,
--
2.5.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* RE: [PATCH] btrfs-progs: fix memory leaks in error path
2015-08-21 3:10 [PATCH] btrfs-progs: fix memory leaks in error path Byongho Lee
@ 2015-08-21 3:50 ` Zhao Lei
2015-08-21 5:32 ` Byongho Lee
2015-08-25 15:36 ` David Sterba
1 sibling, 1 reply; 4+ messages in thread
From: Zhao Lei @ 2015-08-21 3:50 UTC (permalink / raw)
To: 'Byongho Lee', linux-btrfs
Hi, Byongho Lee
> -----Original Message-----
> From: linux-btrfs-owner@vger.kernel.org
> [mailto:linux-btrfs-owner@vger.kernel.org] On Behalf Of Byongho Lee
> Sent: Friday, August 21, 2015 11:10 AM
> To: linux-btrfs@vger.kernel.org
> Subject: [PATCH] btrfs-progs: fix memory leaks in error path
>
> This patch includes below fixes in error path:
> 1. fix memory leaks if realloc() fails
> 2. add missing call free_history() before return error in scrub_read_file()
>
> Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
> ---
> btrfs-list.c | 8 ++++++++
> cmds-scrub.c | 18 ++++++++++++++----
> qgroup.c | 8 ++++++++
Similar problem in cmds-send.c:
cmds-send.c: s->clone_sources = realloc(s->clone_sources,
Thanks
Zhaolei
> 3 files changed, 30 insertions(+), 4 deletions(-)
>
> diff --git a/btrfs-list.c b/btrfs-list.c index 875a89dc4ef0..d54de61aec01 100644
> --- a/btrfs-list.c
> +++ b/btrfs-list.c
> @@ -254,11 +254,15 @@ static int btrfs_list_setup_comparer(struct
> btrfs_list_comparer_set **comp_set,
> BUG_ON(set->ncomps > set->total);
>
> if (set->ncomps == set->total) {
> + void *tmp;
> +
> size = set->total + BTRFS_LIST_NCOMPS_INCREASE;
> size = sizeof(*set) + size * sizeof(struct btrfs_list_comparer);
> + tmp = set;
> set = realloc(set, size);
> if (!set) {
> fprintf(stderr, "memory allocation failed\n");
> + free(tmp);
> exit(1);
> }
>
> @@ -1232,11 +1236,15 @@ int btrfs_list_setup_filter(struct
> btrfs_list_filter_set **filter_set,
> BUG_ON(set->nfilters > set->total);
>
> if (set->nfilters == set->total) {
> + void *tmp;
> +
> size = set->total + BTRFS_LIST_NFILTERS_INCREASE;
> size = sizeof(*set) + size * sizeof(struct btrfs_list_filter);
> + tmp = set;
> set = realloc(set, size);
> if (!set) {
> fprintf(stderr, "memory allocation failed\n");
> + free(tmp);
> exit(1);
> }
>
> diff --git a/cmds-scrub.c b/cmds-scrub.c index 5a85dc473c94..91cf67841849
> 100644
> --- a/cmds-scrub.c
> +++ b/cmds-scrub.c
> @@ -502,12 +502,16 @@ again:
> }
> return p;
> }
> - if (avail == -1)
> + if (avail == -1) {
> + free_history(p);
> return ERR_PTR(-errno);
> + }
> avail += old_avail;
>
> i = 0;
> while (i < avail) {
> + void *tmp;
> +
> switch (state) {
> case 0: /* start of file */
> ret = scrub_kvread(&i,
> @@ -534,11 +538,17 @@ again:
> continue;
> }
> ++curr;
> + tmp = p;
> p = realloc(p, (curr + 2) * sizeof(*p));
> - if (p)
> - p[curr] = malloc(sizeof(**p));
> - if (!p || !p[curr])
> + if (!p) {
> + free_history(tmp);
> return ERR_PTR(-errno);
> + }
> + p[curr] = malloc(sizeof(**p));
> + if (!p[curr]) {
> + free_history(p);
> + return ERR_PTR(-errno);
> + }
> memset(p[curr], 0, sizeof(**p));
> p[curr + 1] = NULL;
> ++state;
> diff --git a/qgroup.c b/qgroup.c
> index dc04b033b145..327abd645f16 100644
> --- a/qgroup.c
> +++ b/qgroup.c
> @@ -465,12 +465,16 @@ int btrfs_qgroup_setup_comparer(struct
> btrfs_qgroup_comparer_set **comp_set,
> BUG_ON(set->ncomps > set->total);
>
> if (set->ncomps == set->total) {
> + void *tmp;
> +
> size = set->total + BTRFS_QGROUP_NCOMPS_INCREASE;
> size = sizeof(*set) +
> size * sizeof(struct btrfs_qgroup_comparer);
> + tmp = set;
> set = realloc(set, size);
> if (!set) {
> fprintf(stderr, "memory allocation failed\n");
> + free(tmp);
> exit(1);
> }
>
> @@ -836,12 +840,16 @@ int btrfs_qgroup_setup_filter(struct
> btrfs_qgroup_filter_set **filter_set,
> BUG_ON(set->nfilters > set->total);
>
> if (set->nfilters == set->total) {
> + void *tmp;
> +
> size = set->total + BTRFS_QGROUP_NFILTERS_INCREASE;
> size = sizeof(*set) + size * sizeof(struct btrfs_qgroup_filter);
>
> + tmp = set;
> set = realloc(set, size);
> if (!set) {
> fprintf(stderr, "memory allocation failed\n");
> + free(tmp);
> exit(1);
> }
> memset(&set->filters[set->total], 0,
> --
> 2.5.0
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body
> of a message to majordomo@vger.kernel.org More majordomo info at
> http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs-progs: fix memory leaks in error path
2015-08-21 3:50 ` Zhao Lei
@ 2015-08-21 5:32 ` Byongho Lee
0 siblings, 0 replies; 4+ messages in thread
From: Byongho Lee @ 2015-08-21 5:32 UTC (permalink / raw)
To: Zhao Lei; +Cc: linux-btrfs
Zhao Lei writes:
> Hi, Byongho Lee
>
>> -----Original Message-----
>> From: linux-btrfs-owner@vger.kernel.org
>> [mailto:linux-btrfs-owner@vger.kernel.org] On Behalf Of Byongho Lee
>> Sent: Friday, August 21, 2015 11:10 AM
>> To: linux-btrfs@vger.kernel.org
>> Subject: [PATCH] btrfs-progs: fix memory leaks in error path
>>
>> This patch includes below fixes in error path:
>> 1. fix memory leaks if realloc() fails
>> 2. add missing call free_history() before return error in scrub_read_file()
>>
>> Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
>> ---
>> btrfs-list.c | 8 ++++++++
>> cmds-scrub.c | 18 ++++++++++++++----
>> qgroup.c | 8 ++++++++
>
>
> Similar problem in cmds-send.c:
> cmds-send.c: s->clone_sources = realloc(s->clone_sources,
>
Thank you for feedback.
You're right, I missed that point.
I'll prepare v2 patch.
Regards,
Lee
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] btrfs-progs: fix memory leaks in error path
2015-08-21 3:10 [PATCH] btrfs-progs: fix memory leaks in error path Byongho Lee
2015-08-21 3:50 ` Zhao Lei
@ 2015-08-25 15:36 ` David Sterba
1 sibling, 0 replies; 4+ messages in thread
From: David Sterba @ 2015-08-25 15:36 UTC (permalink / raw)
To: Byongho Lee; +Cc: linux-btrfs
On Fri, Aug 21, 2015 at 12:10:12PM +0900, Byongho Lee wrote:
> This patch includes below fixes in error path:
> 1. fix memory leaks if realloc() fails
> 2. add missing call free_history() before return error in scrub_read_file()
>
> Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
Applied, thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-08-25 15:36 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-21 3:10 [PATCH] btrfs-progs: fix memory leaks in error path Byongho Lee
2015-08-21 3:50 ` Zhao Lei
2015-08-21 5:32 ` Byongho Lee
2015-08-25 15:36 ` David Sterba
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.