All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge
@ 2025-06-13  4:40 Li Zhijian via
  2025-06-13  7:50 ` Ani Sinha
  2025-06-13  8:07 ` Daniel P. Berrangé
  0 siblings, 2 replies; 4+ messages in thread
From: Li Zhijian via @ 2025-06-13  4:40 UTC (permalink / raw)
  To: qemu-devel; +Cc: imammedo, mst, anisinha, Li Zhijian

This leak was detected by the valgrind.

The crs_range_merge() function unconditionally allocated a GPtrArray
'even when range->len was zero, causing an early return without freeing
the allocated array. This resulted in a memory leak when an empty range
was processed.

Fix this by moving the GPtrArray allocation after the empty range check,
ensuring memory is only allocated when actually needed.

Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
 hw/acpi/aml-build.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
index f8f93a9f66c8..cf1999880119 100644
--- a/hw/acpi/aml-build.c
+++ b/hw/acpi/aml-build.c
@@ -160,7 +160,7 @@ void crs_replace_with_free_ranges(GPtrArray *ranges,
  */
 static void crs_range_merge(GPtrArray *range)
 {
-    GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
+    GPtrArray *tmp;
     CrsRangeEntry *entry;
     uint64_t range_base, range_limit;
     int i;
@@ -169,6 +169,7 @@ static void crs_range_merge(GPtrArray *range)
         return;
     }
 
+    tmp = g_ptr_array_new_with_free_func(crs_range_free);
     g_ptr_array_sort(range, crs_range_compare);
 
     entry = g_ptr_array_index(range, 0);
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge
  2025-06-13  4:40 [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge Li Zhijian via
@ 2025-06-13  7:50 ` Ani Sinha
  2025-06-13  8:07 ` Daniel P. Berrangé
  1 sibling, 0 replies; 4+ messages in thread
From: Ani Sinha @ 2025-06-13  7:50 UTC (permalink / raw)
  To: Li Zhijian; +Cc: qemu-devel, Igor Mammedov, Michael Tsirkin



> On 13 Jun 2025, at 10:10 AM, Li Zhijian <lizhijian@fujitsu.com> wrote:
> 
> This leak was detected by the valgrind.
> 
> The crs_range_merge() function unconditionally allocated a GPtrArray
> 'even when range->len was zero, causing an early return without freeing
> the allocated array. This resulted in a memory leak when an empty range
> was processed.
> 
> Fix this by moving the GPtrArray allocation after the empty range check,
> ensuring memory is only allocated when actually needed.

Thanks for the fix.

> 
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>

Reviewed-by: Ani Sinha <anisinha@redhat.com>

> ---
> hw/acpi/aml-build.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index f8f93a9f66c8..cf1999880119 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -160,7 +160,7 @@ void crs_replace_with_free_ranges(GPtrArray *ranges,
>  */
> static void crs_range_merge(GPtrArray *range)
> {
> -    GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
> +    GPtrArray *tmp;
>     CrsRangeEntry *entry;
>     uint64_t range_base, range_limit;
>     int i;
> @@ -169,6 +169,7 @@ static void crs_range_merge(GPtrArray *range)
>         return;
>     }
> 
> +    tmp = g_ptr_array_new_with_free_func(crs_range_free);
>     g_ptr_array_sort(range, crs_range_compare);
> 
>     entry = g_ptr_array_index(range, 0);
> -- 
> 2.47.0
> 



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge
  2025-06-13  4:40 [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge Li Zhijian via
  2025-06-13  7:50 ` Ani Sinha
@ 2025-06-13  8:07 ` Daniel P. Berrangé
  2025-06-13  8:35   ` Zhijian Li (Fujitsu) via
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel P. Berrangé @ 2025-06-13  8:07 UTC (permalink / raw)
  To: Li Zhijian; +Cc: qemu-devel, imammedo, mst, anisinha

On Fri, Jun 13, 2025 at 12:40:02PM +0800, Li Zhijian via wrote:
> This leak was detected by the valgrind.
> 
> The crs_range_merge() function unconditionally allocated a GPtrArray
> 'even when range->len was zero, causing an early return without freeing
> the allocated array. This resulted in a memory leak when an empty range
> was processed.
> 
> Fix this by moving the GPtrArray allocation after the empty range check,
> ensuring memory is only allocated when actually needed.
> 
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
> ---
>  hw/acpi/aml-build.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
> index f8f93a9f66c8..cf1999880119 100644
> --- a/hw/acpi/aml-build.c
> +++ b/hw/acpi/aml-build.c
> @@ -160,7 +160,7 @@ void crs_replace_with_free_ranges(GPtrArray *ranges,
>   */
>  static void crs_range_merge(GPtrArray *range)
>  {
> -    GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
> +    GPtrArray *tmp;

IMHO it would be better to change this to

  g_autoptr(GPtrArray) tmp = g_ptr....


and remove the existing manual g_ptr_array_free call. This guarantees
it will always be released in every code path.

>      CrsRangeEntry *entry;
>      uint64_t range_base, range_limit;
>      int i;
> @@ -169,6 +169,7 @@ static void crs_range_merge(GPtrArray *range)
>          return;
>      }
>  
> +    tmp = g_ptr_array_new_with_free_func(crs_range_free);
>      g_ptr_array_sort(range, crs_range_compare);
>  
>      entry = g_ptr_array_index(range, 0);
> -- 
> 2.47.0
> 
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge
  2025-06-13  8:07 ` Daniel P. Berrangé
@ 2025-06-13  8:35   ` Zhijian Li (Fujitsu) via
  0 siblings, 0 replies; 4+ messages in thread
From: Zhijian Li (Fujitsu) via @ 2025-06-13  8:35 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: qemu-devel@nongnu.org, imammedo@redhat.com, mst@redhat.com,
	anisinha@redhat.com



On 13/06/2025 16:07, Daniel P. Berrangé wrote:
> On Fri, Jun 13, 2025 at 12:40:02PM +0800, Li Zhijian via wrote:
>> This leak was detected by the valgrind.
>>
>> The crs_range_merge() function unconditionally allocated a GPtrArray
>> 'even when range->len was zero, causing an early return without freeing
>> the allocated array. This resulted in a memory leak when an empty range
>> was processed.
>>
>> Fix this by moving the GPtrArray allocation after the empty range check,
>> ensuring memory is only allocated when actually needed.
>>
>> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
>> ---
>>   hw/acpi/aml-build.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c
>> index f8f93a9f66c8..cf1999880119 100644
>> --- a/hw/acpi/aml-build.c
>> +++ b/hw/acpi/aml-build.c
>> @@ -160,7 +160,7 @@ void crs_replace_with_free_ranges(GPtrArray *ranges,
>>    */
>>   static void crs_range_merge(GPtrArray *range)
>>   {
>> -    GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
>> +    GPtrArray *tmp;
> 
> IMHO it would be better to change this to
> 
>    g_autoptr(GPtrArray) tmp = g_ptr....
> 
> 
> and remove the existing manual g_ptr_array_free call. This guarantees
> it will always be released in every code path.


It sounds good to me, I will update it soon.

Thank


> 
>>       CrsRangeEntry *entry;
>>       uint64_t range_base, range_limit;
>>       int i;
>> @@ -169,6 +169,7 @@ static void crs_range_merge(GPtrArray *range)
>>           return;
>>       }
>>   
>> +    tmp = g_ptr_array_new_with_free_func(crs_range_free);
>>       g_ptr_array_sort(range, crs_range_compare);
>>   
>>       entry = g_ptr_array_index(range, 0);
>> -- 
>> 2.47.0
>>
>>
> 
> With regards,
> Daniel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-13  8:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-13  4:40 [PATCH] hw/acpi: Fix GPtrArray memory leak in crs_range_merge Li Zhijian via
2025-06-13  7:50 ` Ani Sinha
2025-06-13  8:07 ` Daniel P. Berrangé
2025-06-13  8:35   ` Zhijian Li (Fujitsu) via

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.