This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new dd5670ed65b drivers/analog: Fix memory leak and dead code in 
mcp3008_initialize.
dd5670ed65b is described below

commit dd5670ed65b53387a7e1e11019f1b55d0201dfc5
Author: hanzj <[email protected]>
AuthorDate: Thu May 28 16:18:36 2026 +0800

    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 <[email protected]>
---
 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;
     }
 

Reply via email to