diff mbox series

[5/5,v2] get_vd_num_of_subarray: fix memleak

Message ID 62b32172-f3b5-8c32-0a74-77e8b18927d1@huawei.com (mailing list archive)
State Superseded, archived
Headers show
Series mdadm: fix memory leak and double free | expand

Commit Message

Wu Guanghao Aug. 2, 2022, 2:17 a.m. UTC
sra = sysfs_read() should be free before return in
get_vd_num_of_subarray()

Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
---
 super-ddf.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

--
2.27.0

Comments

Coly Li Sept. 3, 2022, 8:25 a.m. UTC | #1
> 2022年8月2日 10:17,Wu Guanghao <wuguanghao3@huawei.com> 写道:
> 
> sra = sysfs_read() should be free before return in
> get_vd_num_of_subarray()
> 
> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
> Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>

Hi Guanghao,

I have a question for this patch, please correct me if I am wrong.



> ---
> super-ddf.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/super-ddf.c b/super-ddf.c
> index abbc8b09..6d4618fe 100644
> --- a/super-ddf.c
> +++ b/super-ddf.c
> @@ -1599,15 +1599,20 @@ static unsigned int get_vd_num_of_subarray(struct supertype *st)
>        sra = sysfs_read(-1, st->devnm, GET_VERSION);
>        if (!sra || sra->array.major_version != -1 ||
>            sra->array.minor_version != -2 ||
> -           !is_subarray(sra->text_version))
> +           !is_subarray(sra->text_version)) {
> +               if (sra)
> +                       sysfs_free(sra);
>                return DDF_NOTFOUND;
> +       }
> 
>        sub = strchr(sra->text_version + 1, '/');
>        if (sub != NULL)
>                vcnum = strtoul(sub + 1, &end, 10);
>        if (sub == NULL || *sub == '\0' || *end != '\0' ||
> -           vcnum >= be16_to_cpu(ddf->active->max_vd_entries))
> +           vcnum >= be16_to_cpu(ddf->active->max_vd_entries)) {
> +               sysfs_free(sra);
>                return DDF_NOTFOUND;
> +       }
> 
>        return vcnum;


Before return vcnum, should call sysfs_free(sra) ?

Thank you in advance.

Coly Li



> }
> --
> 2.27.0
Coly Li Sept. 14, 2022, 2:56 p.m. UTC | #2
> 2022年9月3日 16:25,Coly Li <colyli@suse.de> 写道:
> 
> 
> 
>> 2022年8月2日 10:17,Wu Guanghao <wuguanghao3@huawei.com> 写道:
>> 
>> sra = sysfs_read() should be free before return in
>> get_vd_num_of_subarray()
>> 
>> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
>> Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
> 
> Hi Guanghao,
> 
> I have a question for this patch, please correct me if I am wrong.
> 
> 
> 
>> ---
>> super-ddf.c | 9 +++++++--
>> 1 file changed, 7 insertions(+), 2 deletions(-)
>> 
>> diff --git a/super-ddf.c b/super-ddf.c
>> index abbc8b09..6d4618fe 100644
>> --- a/super-ddf.c
>> +++ b/super-ddf.c
>> @@ -1599,15 +1599,20 @@ static unsigned int get_vd_num_of_subarray(struct supertype *st)
>>       sra = sysfs_read(-1, st->devnm, GET_VERSION);
>>       if (!sra || sra->array.major_version != -1 ||
>>           sra->array.minor_version != -2 ||
>> -           !is_subarray(sra->text_version))
>> +           !is_subarray(sra->text_version)) {
>> +               if (sra)
>> +                       sysfs_free(sra);
>>               return DDF_NOTFOUND;
>> +       }
>> 
>>       sub = strchr(sra->text_version + 1, '/');
>>       if (sub != NULL)
>>               vcnum = strtoul(sub + 1, &end, 10);
>>       if (sub == NULL || *sub == '\0' || *end != '\0' ||
>> -           vcnum >= be16_to_cpu(ddf->active->max_vd_entries))
>> +           vcnum >= be16_to_cpu(ddf->active->max_vd_entries)) {
>> +               sysfs_free(sra);
>>               return DDF_NOTFOUND;
>> +       }
>> 
>>       return vcnum;
> 
> 
> Before return vcnum, should call sysfs_free(sra) ?
> 
> Thank you in advance.

Ping?

Coly Li
diff mbox series

Patch

diff --git a/super-ddf.c b/super-ddf.c
index abbc8b09..6d4618fe 100644
--- a/super-ddf.c
+++ b/super-ddf.c
@@ -1599,15 +1599,20 @@  static unsigned int get_vd_num_of_subarray(struct supertype *st)
        sra = sysfs_read(-1, st->devnm, GET_VERSION);
        if (!sra || sra->array.major_version != -1 ||
            sra->array.minor_version != -2 ||
-           !is_subarray(sra->text_version))
+           !is_subarray(sra->text_version)) {
+               if (sra)
+                       sysfs_free(sra);
                return DDF_NOTFOUND;
+       }

        sub = strchr(sra->text_version + 1, '/');
        if (sub != NULL)
                vcnum = strtoul(sub + 1, &end, 10);
        if (sub == NULL || *sub == '\0' || *end != '\0' ||
-           vcnum >= be16_to_cpu(ddf->active->max_vd_entries))
+           vcnum >= be16_to_cpu(ddf->active->max_vd_entries)) {
+               sysfs_free(sra);
                return DDF_NOTFOUND;
+       }

        return vcnum;
 }