[Concept,16/17] dm: acpi: Fix memory leaks in ACPI item tracking and tests

Message ID 20260316183050.3855921-17-sjg@u-boot.org
State New
Headers
Series Add automatic memory-leak detection to U-Boot tests |

Commit Message

Simon Glass March 16, 2026, 6:30 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

acpi_reset_items() resets the item count without freeing the buffers
allocated by add_item(). Free them before resetting.

Also add missing free(buf) calls in dm_test_acpi_fill_ssdt(),
dm_test_acpi_fill_madt() and dm_test_acpi_inject_dsdt() which allocate a
buffer with malloc() but never free it.

Fixes: 18434aec1b69 ("acpi: Don't reset the tables with every new generation")
Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/core/acpi.c | 6 ++++++
 test/dm/acpi.c      | 6 ++++++
 2 files changed, 12 insertions(+)
  

Patch

diff --git a/drivers/core/acpi.c b/drivers/core/acpi.c
index 4763963914b..efb935ed6a1 100644
--- a/drivers/core/acpi.c
+++ b/drivers/core/acpi.c
@@ -377,6 +377,12 @@  int acpi_inject_dsdt(struct acpi_ctx *ctx)
 
 void acpi_reset_items(void)
 {
+	int i;
+
+	for (i = 0; i < item_count; i++) {
+		free(acpi_item[i].buf);
+		acpi_item[i].buf = NULL;
+	}
 	item_count = 0;
 }
 
diff --git a/test/dm/acpi.c b/test/dm/acpi.c
index 588a518bc4f..9eca5df967b 100644
--- a/test/dm/acpi.c
+++ b/test/dm/acpi.c
@@ -596,6 +596,8 @@  static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
 
 	ut_asserteq('z', buf[4]);
 
+	free(buf);
+
 	return 0;
 }
 DM_TEST(dm_test_acpi_fill_ssdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
@@ -622,6 +624,8 @@  static int dm_test_acpi_fill_madt(struct unit_test_state *uts)
 
 	ut_asserteq('z', buf[1]);
 
+	free(buf);
+
 	return 0;
 }
 
@@ -654,6 +658,8 @@  static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
 
 	ut_asserteq('z', buf[4]);
 
+	free(buf);
+
 	return 0;
 }
 DM_TEST(dm_test_acpi_inject_dsdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);