tools/nolibc/stdio: make printf(%s) accept NULL
authorWilly Tarreau <w@1wt.eu>
Mon, 21 Mar 2022 17:33:07 +0000 (18:33 +0100)
committerPaul E. McKenney <paulmck@kernel.org>
Thu, 21 Apr 2022 00:05:45 +0000 (17:05 -0700)
It's often convenient to support this, especially in test programs where
a NULL may correspond to an allocation error or a non-existing value.
Let's make printf("%s") support being passed a NULL. In this case it
prints "(null)" like glibc's printf().

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/stdio.h

index cb4d3ab3a5656b190596037f0b04a21bc385be0b..559ebe052a75700e337e3e21adbe0864cfe25baf 100644 (file)
@@ -220,6 +220,8 @@ int vfprintf(FILE *stream, const char *fmt, va_list args)
                        }
                        else if (c == 's') {
                                outstr = va_arg(args, char *);
+                               if (!outstr)
+                                       outstr="(null)";
                        }
                        else if (c == '%') {
                                /* queue it verbatim */