From: Li Zhijian Date: Fri, 13 Jun 2025 08:51:10 +0000 (+0800) Subject: hw/acpi: Fix GPtrArray memory leak in crs_range_merge X-Git-Tag: v10.1.0-rc0~19^2~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7fb5693d9ffbeb9b49c981e1f9774392f1d41e5;p=thirdparty%2Fqemu.git hw/acpi: Fix GPtrArray memory leak in crs_range_merge 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. Instead of moving the allocation after the check (as previously attempted), use g_autoptr for automatic cleanup. This ensures the array is freed even on early returns, and also removes the need for the explicit free at the end of the function. Signed-off-by: Li Zhijian Message-Id: <20250613085110.111204-1-lizhijian@fujitsu.com> Reviewed-by: Daniel P. Berrangé Reviewed-by: Ani Sinha Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index f8f93a9f66..cb817a0f31 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); + g_autoptr(GPtrArray) tmp = g_ptr_array_new_with_free_func(crs_range_free); CrsRangeEntry *entry; uint64_t range_base, range_limit; int i; @@ -191,7 +191,6 @@ static void crs_range_merge(GPtrArray *range) entry = g_ptr_array_index(tmp, i); crs_range_insert(range, entry->base, entry->limit); } - g_ptr_array_free(tmp, true); } static void