From dd5670ed65b53387a7e1e11019f1b55d0201dfc5 Mon Sep 17 00:00:00 2001 From: hanzj Date: Thu, 28 May 2026 16:18:36 +0800 Subject: [PATCH] drivers/analog: Fix memory leak and dead code in mcp3008_initialize. Fix two bugs in mcp3008_initialize(): 1. Remove dead free(priv) when priv is NULL (line 382): The first allocation checks if priv == NULL, then calls free(priv) which is a no-op since priv is NULL. Remove the dead call. 2. Add missing kmm_free(priv) when adcdev allocation fails (line 396): If the second kmm_malloc() for adcdev fails, the function returns NULL without freeing the already-allocated priv, causing a memory leak. Add kmm_free(priv) before the return. Signed-off-by: hanzj --- drivers/analog/mcp3008.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/analog/mcp3008.c b/drivers/analog/mcp3008.c index ce56b9899f3..a3610db8a53 100644 --- a/drivers/analog/mcp3008.c +++ b/drivers/analog/mcp3008.c @@ -379,7 +379,6 @@ FAR struct adc_dev_s *mcp3008_initialize(FAR struct spi_dev_s *spi) if (priv == NULL) { aerr("ERROR: Failed to allocate mcp3008_dev_s instance\n"); - free(priv); return NULL; } @@ -393,6 +392,7 @@ FAR struct adc_dev_s *mcp3008_initialize(FAR struct spi_dev_s *spi) if (adcdev == NULL) { aerr("ERROR: Failed to allocate adc_dev_s instance\n"); + kmm_free(priv); return NULL; }