Skip to contentSkip to conversation

tiny-printf: handle NULL pointer for %s format string

PATCH New [email protected] 1 participant 1 message May 19, 2025 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tiny-printf: handle NULL pointer for %s format string

From: Anton Moryakov <[email protected]>

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.

---
 lib/tiny-printf.c | 2 ++
 1 file changed, 2 insertions(+)
lib/tiny-printf.c +2 -0
index 2a7a4d286c0..df5f6829db5 100644
@@ -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, '%');
Signature · 1 line
2.34.1