All of lore.kernel.org
 help / color / mirror / Atom feed
From: ant.v.moryakov@gmail.com
To: u-boot@lists.denx.de
Cc: trini@konsulko.com, Anton Moryakov <ant.v.moryakov@gmail.com>
Subject: [PATCH] drivers: spi: fix deref ater null.might in spi-uclass.c
Date: Fri, 16 May 2025 15:50:53 +0300	[thread overview]
Message-ID: <20250516125053.25457-1-ant.v.moryakov@gmail.com> (raw)

From: Anton Moryakov <ant.v.moryakov@gmail.com>

The static analyzer (Svace) reported 
After having been compared to a NULL value at spi-uclass.c:465,
pointer 'dev' is passed as 1st parameter in call to function 'dev_get_flags' 
at spi-uclass.c:469, where it is dereferenced at device.h:240.

Correct explained:
1. Added dev && !device_active(dev) check before calling device_active()
2. Added explicit if (!dev) check with ret = -ENODEV setting
3. Protected logging in error block with if(dev) check

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
 drivers/spi/spi-uclass.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c
index d6049753740..52b79223f96 100644
--- a/drivers/spi/spi-uclass.c
+++ b/drivers/spi/spi-uclass.c
@@ -345,7 +345,7 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
 		return ret;
 	}
 
-	if (!device_active(dev)) {
+	if (dev && !device_active(dev)) {
 		struct spi_slave *slave;
 
 		ret = device_probe(dev);
@@ -355,6 +355,11 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
 		slave->dev = dev;
 	}
 
+	if (!dev) {
+		ret = -ENODEV;
+		goto err;
+	}
+
 	slave = dev_get_parent_priv(dev);
 	bus_data = dev_get_uclass_priv(bus);
 
@@ -373,8 +378,11 @@ int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
 	return 0;
 
 err:
-	log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
-
+	if(dev)
+		log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
+	else
+		log_debug("%s: Error path, NULL device\n", __func__);
+	
 	return ret;
 }
 
-- 
2.30.2


             reply	other threads:[~2025-05-16 12:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-16 12:50 ant.v.moryakov [this message]
2025-05-16 15:56 ` [PATCH] drivers: spi: fix deref ater null.might in spi-uclass.c Tom Rini
2025-05-16 16:20 ` Simon Glass
2025-05-16 16:24   ` Anton Moryakov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250516125053.25457-1-ant.v.moryakov@gmail.com \
    --to=ant.v.moryakov@gmail.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.