[PATCH] lib: add NULL pointer check for %s format

Tom Rini trini at konsulko.com
Mon May 19 21:11:16 CEST 2025


On Fri, May 16, 2025 at 09:54:47PM +0300, ant.v.moryakov at gmail.com wrote:

> From: Anton Moryakov <ant.v.moryakov at gmail.com>
> 
> 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.
> 
> Signed-off-by: Anton Moryakov <ant.v.moryakov at gmail.com>"

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 | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/lib/tiny-printf.c b/lib/tiny-printf.c
> index faf55d7f327..da596d72050 100644
> --- a/lib/tiny-printf.c
> +++ b/lib/tiny-printf.c
> @@ -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).

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20250519/0ec27d12/attachment.sig>


More information about the U-Boot mailing list