[PATCH v2] tiny-printf: handle NULL pointer for %s format string
From: ant . v . moryakov
Date: Mon May 19 2025 - 15:26:11 EST
From: Anton Moryakov <ant.v.moryakov@xxxxxxxxx>
Avoid NULL pointer dereference in string formatting by printing "(null)"
when a NULL pointer is passed to a %s format specifier.
This change makes the behavior consistent with standard printf()
implementations and prevents potential crashes in constrained environments.
Signed-off-by: Anton Moryakov <ant.v.moryakov@xxxxxxxxx>
---
lib/tiny-printf.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c
index 2a7a4d286c0..df5f6829db5 100644
--- a/lib/tiny-printf.c
+++ b/lib/tiny-printf.c
@@ -307,6 +307,8 @@ static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
break;
case 's':
p = va_arg(va, char*);
+ if (!p)
+ p = "(null)";
break;
case '%':
out(info, '%');
--
2.34.1