* [PATCH master] common: console_common: handle NULL dev gracefully in dev_printf
@ 2024-03-13 9:26 Ahmad Fatoum
2024-03-13 10:50 ` Sascha Hauer
0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2024-03-13 9:26 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH master] common: console_common: handle NULL dev gracefully in dev_printf
2024-03-13 9:26 [PATCH master] common: console_common: handle NULL dev gracefully in dev_printf Ahmad Fatoum
@ 2024-03-13 10:50 ` Sascha Hauer
0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2024-03-13 10:50 UTC (permalink / raw)
To: barebox, Ahmad Fatoum
On Wed, 13 Mar 2024 10:26:33 +0100, Ahmad Fatoum wrote:
> 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.
>
> [...]
Applied, thanks!
[1/1] common: console_common: handle NULL dev gracefully in dev_printf
https://git.pengutronix.de/cgit/barebox/commit/?id=d20abb491654 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-03-13 10:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-13 9:26 [PATCH master] common: console_common: handle NULL dev gracefully in dev_printf Ahmad Fatoum
2024-03-13 10:50 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox