[Qemu-devel] [PATCH 1/3] tests/migration: mem leak fix

Mao Zhongyi posted 3 patches 6 years, 5 months ago
There is a newer version of this series
[Qemu-devel] [PATCH 1/3] tests/migration: mem leak fix
Posted by Mao Zhongyi 6 years, 5 months ago
Cc: armbru@redhat.com
Cc: laurent@vivier.eu
Cc: tony.nguyen@bt.com

Signed-off-by: Mao Zhongyi <maozhongyi@cmss.chinamobile.com>
---
 tests/migration/stress.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tests/migration/stress.c b/tests/migration/stress.c
index d9aa4afe92..e6c9a6b243 100644
--- a/tests/migration/stress.c
+++ b/tests/migration/stress.c
@@ -181,6 +181,8 @@ static int stressone(unsigned long long ramsizeMB)
     if (!ram) {
         fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
                 argv0, gettid(), ramsizeMB, strerror(errno));
+        if (data)
+            free(data);
         return -1;
     }
     if (!data) {
-- 
2.17.1




Re: [Qemu-devel] [PATCH 1/3] tests/migration: mem leak fix
Posted by Alex Bennée 6 years, 5 months ago
Mao Zhongyi <maozhongyi@cmss.chinamobile.com> writes:

> Cc: armbru@redhat.com
> Cc: laurent@vivier.eu
> Cc: tony.nguyen@bt.com
>
> Signed-off-by: Mao Zhongyi <maozhongyi@cmss.chinamobile.com>
> ---
>  tests/migration/stress.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/tests/migration/stress.c b/tests/migration/stress.c
> index d9aa4afe92..e6c9a6b243 100644
> --- a/tests/migration/stress.c
> +++ b/tests/migration/stress.c
> @@ -181,6 +181,8 @@ static int stressone(unsigned long long ramsizeMB)
>      if (!ram) {
>          fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
>                  argv0, gettid(), ramsizeMB, strerror(errno));
> +        if (data)
> +            free(data);

I wonder if it's worth using the glib macros here so:

  g_autofree char *data = g_malloc(PAGE_SIZE);

and the same for ram. You can then drop the frees.

>          return -1;
>      }
>      if (!data) {


--
Alex Bennée

Re: [Qemu-devel] [PATCH 1/3] tests/migration: mem leak fix
Posted by maozy 6 years, 5 months ago
On 9/11/19 2:52 AM, Alex Bennée wrote:
> Mao Zhongyi <maozhongyi@cmss.chinamobile.com> writes:
>
>> Cc: armbru@redhat.com
>> Cc: laurent@vivier.eu
>> Cc: tony.nguyen@bt.com
>>
>> Signed-off-by: Mao Zhongyi <maozhongyi@cmss.chinamobile.com>
>> ---
>>   tests/migration/stress.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/tests/migration/stress.c b/tests/migration/stress.c
>> index d9aa4afe92..e6c9a6b243 100644
>> --- a/tests/migration/stress.c
>> +++ b/tests/migration/stress.c
>> @@ -181,6 +181,8 @@ static int stressone(unsigned long long ramsizeMB)
>>       if (!ram) {
>>           fprintf(stderr, "%s (%05d): ERROR: cannot allocate %llu MB of RAM: %s\n",
>>                   argv0, gettid(), ramsizeMB, strerror(errno));
>> +        if (data)
>> +            free(data);
> I wonder if it's worth using the glib macros here so:
>
>    g_autofree char *data = g_malloc(PAGE_SIZE);
>
> and the same for ram. You can then drop the frees.
>
I thins it's ok, which is also recommended in CODING_STYLE.rst.

Thx
     Mao
>>           return -1;
>>       }
>>       if (!data) {
>
> --
> Alex Bennée
>