drivers/analog/dac7554: Add NULL checks after kmm_malloc in dac7554_initialize

dac7554_initialize() calls kmm_malloc twice without checking the return
value.  If either allocation fails, the subsequent pointer dereferences
lead to a NULL pointer access and crash.

Add NULL checks for both allocations, following the pattern already used
in mcp3008.c, mcp48xx.c, and mcp47x6.c.  When the second allocation
fails, free the first allocation before returning NULL.

Signed-off-by: hanzj <hanzjian@zepp.com>
This commit is contained in:
hanzj 2026-05-30 07:29:38 +08:00 committed by Lup Yuen Lee
parent 283742e29c
commit 36bdf9fb37

View file

@ -248,10 +248,23 @@ FAR struct dac_dev_s *dac7554_initialize(FAR struct spi_dev_s *spi,
/* Initialize the DAC7554 device structure */
priv = kmm_malloc(sizeof(struct dac7554_dev_s));
if (priv == NULL)
{
aerr("ERROR: Failed to allocate dac7554_dev_s instance\n");
return NULL;
}
priv->spi = spi;
priv->spidev = spidev;
g_dacdev = kmm_malloc(sizeof(struct dac_dev_s));
if (g_dacdev == NULL)
{
aerr("ERROR: Failed to allocate dac_dev_s instance\n");
kmm_free(priv);
return NULL;
}
g_dacdev->ad_ops = &g_dacops;
g_dacdev->ad_priv = priv;