[Concept,19/33] bootctl: simple_state: Free items alist on remove

Message ID 20260416023021.626949-20-sjg@u-boot.org
State New
Headers
Series Fix memory leaks and test pollution in sandbox tests |

Commit Message

Simon Glass April 16, 2026, 2:29 a.m. UTC
  From: Simon Glass <sjg@chromium.org>

The simple_state driver accumulates key/value pairs in priv->items,
each with allocated key and val

Nothing releases them when the device is removed, so a test that calls
write_val() repeatedly leaks hundreds of entries.

Add a remove() method to call clear_vals() and tear down the alist.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 boot/bootctl/simple_state.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
  

Patch

diff --git a/boot/bootctl/simple_state.c b/boot/bootctl/simple_state.c
index 062bb697fbd..84da799e84a 100644
--- a/boot/bootctl/simple_state.c
+++ b/boot/bootctl/simple_state.c
@@ -435,6 +435,16 @@  static const struct udevice_id sstate_ids[] = {
 	{ }
 };
 
+static int sstate_remove(struct udevice *dev)
+{
+	struct sstate_priv *priv = dev_get_priv(dev);
+
+	clear_vals(priv);
+	alist_uninit(&priv->items);
+
+	return 0;
+}
+
 U_BOOT_DRIVER(simple_state) = {
 	.name		= "simple_state",
 	.id		= UCLASS_BOOTCTL_STATE,
@@ -442,6 +452,7 @@  U_BOOT_DRIVER(simple_state) = {
 	.ops		= &ops,
 	.bind		= sstate_bind,
 	.probe		= sstate_probe,
+	.remove		= sstate_remove,
 	.of_to_plat	= sstate_of_to_plat,
 	.priv_auto	= sizeof(struct sstate_priv),
 };