From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master] common: console_common: handle NULL dev gracefully in dev_printf
Date: Wed, 13 Mar 2024 10:26:33 +0100 [thread overview]
Message-ID: <20240313092633.3339320-1-a.fatoum@pengutronix.de> (raw)
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@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
next reply other threads:[~2024-03-13 9:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-13 9:26 Ahmad Fatoum [this message]
2024-03-13 10:50 ` Sascha Hauer
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=20240313092633.3339320-1-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox