[PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function

Peter Maydell posted 3 patches 4 years, 7 months ago
Maintainers: Thomas Huth <thuth@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Corey Minyard <minyard@acm.org>, Stefan Berger <stefanb@linux.vnet.ibm.com>, Laurent Vivier <lvivier@redhat.com>
[PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function
Posted by Peter Maydell 4 years, 7 months ago
tpm_util_migration_start_qemu() allocates memory with g_strdup_printf()
but frees it with free() rather than g_free(), which provokes Coverity
complaints (CID 1432379, 1432350). Use the correct free function.

Fixes: Coverity CID 1432379, CID 1432350
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
With newer glib (2.46 and up) g_free() is guaranteed to be the same
as free(), but matching things up is neater anyway.
---
 tests/qtest/tpm-util.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/tpm-util.c b/tests/qtest/tpm-util.c
index b70cc32d600..3a40ff3f96c 100644
--- a/tests/qtest/tpm-util.c
+++ b/tests/qtest/tpm-util.c
@@ -289,6 +289,6 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu,
 
     *dst_qemu = qtest_init(dst_qemu_args);
 
-    free(src_qemu_args);
-    free(dst_qemu_args);
+    g_free(src_qemu_args);
+    g_free(dst_qemu_args);
 }
-- 
2.20.1


Re: [PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function
Posted by Stefan Berger 4 years, 7 months ago
On 5/3/21 12:55 PM, Peter Maydell wrote:
> tpm_util_migration_start_qemu() allocates memory with g_strdup_printf()
> but frees it with free() rather than g_free(), which provokes Coverity
> complaints (CID 1432379, 1432350). Use the correct free function.
>
> Fixes: Coverity CID 1432379, CID 1432350
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> With newer glib (2.46 and up) g_free() is guaranteed to be the same
> as free(), but matching things up is neater anyway.
> ---
>   tests/qtest/tpm-util.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tests/qtest/tpm-util.c b/tests/qtest/tpm-util.c
> index b70cc32d600..3a40ff3f96c 100644
> --- a/tests/qtest/tpm-util.c
> +++ b/tests/qtest/tpm-util.c
> @@ -289,6 +289,6 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu,
>
>       *dst_qemu = qtest_init(dst_qemu_args);
>
> -    free(src_qemu_args);
> -    free(dst_qemu_args);
> +    g_free(src_qemu_args);
> +    g_free(dst_qemu_args);
>   }


Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>



Re: [PATCH 1/3] tests/qtest/tpm-util.c: Free memory with correct free function
Posted by Alex Bennée 4 years, 7 months ago
Peter Maydell <peter.maydell@linaro.org> writes:

> tpm_util_migration_start_qemu() allocates memory with g_strdup_printf()
> but frees it with free() rather than g_free(), which provokes Coverity
> complaints (CID 1432379, 1432350). Use the correct free function.
>
> Fixes: Coverity CID 1432379, CID 1432350
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> With newer glib (2.46 and up) g_free() is guaranteed to be the same
> as free(), but matching things up is neater anyway.
> ---
>  tests/qtest/tpm-util.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tests/qtest/tpm-util.c b/tests/qtest/tpm-util.c
> index b70cc32d600..3a40ff3f96c 100644
> --- a/tests/qtest/tpm-util.c
> +++ b/tests/qtest/tpm-util.c
> @@ -289,6 +289,6 @@ void tpm_util_migration_start_qemu(QTestState **src_qemu,
>  
>      *dst_qemu = qtest_init(dst_qemu_args);
>  
> -    free(src_qemu_args);
> -    free(dst_qemu_args);
> +    g_free(src_qemu_args);
> +    g_free(dst_qemu_args);
>  }

The more modern approach would be to use g_autofree but this works given
we aren't dealing with multiple exits:

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

-- 
Alex Bennée