Skip to contentSkip to conversation

lib: add NULL pointer check for %s format

PATCH In review [email protected] 2 participants 3 messages May 16, 2025 – May 19, 2025 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: add NULL pointer check for %s format

From: Anton Moryakov <[email protected]>

Handle NULL pointer case in string formatting (%s) by printing '(null)' instead of dereferencing NULL pointer. Makes behavior consistent with standard printf implementations and prevents potential crashes.

---
 lib/tiny-printf.c | 2 ++
 1 file changed, 2 insertions(+)
lib/tiny-printf.c +2 -0
index faf55d7f327..da596d72050 100644
@@ -306,6 +306,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.30.2
#1 lore raw
Quoted 7 lines · On Fri, May 16, 2025 at 09:54:47PM +0300, [email protected] wrote:

From: Anton Moryakov <[email protected]>

Handle NULL pointer case in string formatting (%s) by printing '(null)' instead of dereferencing NULL pointer. Makes behavior consistent with standard printf implementations and prevents potential crashes.

First, this is tiny-printf so the subject should be changed to be clearer about the impact. This is only used in very constrained environments.

Quoted 9 lines
---
 lib/tiny-printf.c | 2 ++
 1 file changed, 2 insertions(+)
lib/tiny-printf.c +0 -0
index faf55d7f327..da596d72050 100644

lib/tiny-printf.c static int _vprintf(struct printf_info *info, const char *fmt, va_list va) · line 313

Quoted 8 lines
lib/tiny-printf.c +2 -0
@@ -306,6 +306,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, '%');

It's so constrained that I'd rather not include "(null)". We should end up printing nothing in this case, as this is handled in commit f0dab28915b7 ("tiny-printf: Handle NULL pointer argument to %s") and having a print where we just omit something, in this constrained environment, is acceptable (and should be the debug hint needed).

Signature · 1 line
Tom
#2 lore raw

Hi Tom,

Thank you for your feedback.

You're absolutely right — the subject and commit message were too generic and wordy. I've revised the patch accordingly and sent a v2 with the subject:

[PATCH v2] tiny-printf: handle NULL pointer for %s format string

The v2 includes:
- Updated subject prefix to reflect the correct subsystem (tiny-printf),
- Simplified and clarified the commit message as per your suggestions,
- No code changes — just improved presentation.

Thanks again for the helpful review!

Best regards,
Anton Moryakov

пн, 19 мая 2025 г. в 22:11, Tom Rini <[email protected]>:

Quoted 42 lines · On Fri, May 16, 2025 at 09:54:47PM +0300, [email protected] wrote:
Quoted 8 lines

From: Anton Moryakov <[email protected]>

Handle NULL pointer case in string formatting (%s) by printing '(null)' instead of dereferencing NULL pointer. Makes behavior consistent with standard printf implementations and prevents potential crashes.

First, this is tiny-printf so the subject should be changed to be clearer about the impact. This is only used in very constrained environments.

lib/tiny-printf.c static int _vprintf(struct printf_info *info, const

Quoted 9 lines
---
 lib/tiny-printf.c | 2 ++
 1 file changed, 2 insertions(+)
lib/tiny-printf.c +0 -0
index faf55d7f327..da596d72050 100644
@@ -306,6 +306,8 @@ static int _vprintf(struct printf_info *info, const

char *fmt, va_list va)

lib/tiny-printf.c static int _vprintf(struct printf_info *info, const char *fmt, va_list va) · line 313

Quoted 8 lines
lib/tiny-printf.c +2 -0
                              break;
                      case 's':
                              p = va_arg(va, char*);
+                             if (!p)
+                                     p = "(null)";
                              break;
                      case '%':
                              out(info, '%');

It's so constrained that I'd rather not include "(null)". We should end up printing nothing in this case, as this is handled in commit f0dab28915b7 ("tiny-printf: Handle NULL pointer argument to %s") and having a print where we just omit something, in this constrained environment, is acceptable (and should be the debug hint needed).

--
Tom