[PATCH master] common: console_common: handle NULL dev gracefully in dev_printf

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Mar 13 02:26:33 PDT 2024


dev_name(NULL) returns NULL, which snprintf can handle, but dev->driver
used beforehand would derference the NULL pointer without checking.

Fix this by checking that dev is !NULL before dereferencing.
This still leads to an ugly <NULL>: prefix, when a NULL dev is used, so
it can be fixed, but without the crash, which might be difficult to
pinpoint if the print happens early.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/console_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/console_common.c b/common/console_common.c
index 631756e4800c..0113a6413850 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -146,7 +146,7 @@ int dev_printf(int level, const struct device *dev, const char *format, ...)
 	if (!IS_ENABLED(CONFIG_LOGBUF) && level > barebox_loglevel)
 		return 0;
 
-	if (dev->driver && dev->driver->name)
+	if (dev && dev->driver && dev->driver->name)
 		ret += snprintf(printbuffer, size, "%s ", dev->driver->name);
 
 	ret += snprintf(printbuffer + ret, size - ret, "%s: ", dev_name(dev));
-- 
2.39.2




More information about the barebox mailing list