]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[libc] Print "<NULL>" for wide-character NULL strings
authorMichael Brown <mcb30@ipxe.org>
Tue, 12 Apr 2016 10:51:05 +0000 (11:51 +0100)
committerMichael Brown <mcb30@ipxe.org>
Tue, 12 Apr 2016 10:53:06 +0000 (11:53 +0100)
The existing code intends to print NULL strings as "<NULL>" (for the
sake of debug messages), but the logic is incorrect when handling
wide-character strings.  Fix the logic and add applicable unit tests.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/vsprintf.c
src/tests/vsprintf_test.c

index cb3bec5ddd62442f089dc477e58dbbac73570dee..9d3a97c2df33e2ca1bf116822767aa7c06daed6e 100644 (file)
@@ -257,11 +257,13 @@ size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
                } else if ( *fmt == 's' ) {
                        if ( length < &type_sizes[LONG_LEN] ) {
                                ptr = va_arg ( args, char * );
+                               if ( ! ptr )
+                                       ptr = "<NULL>";
                        } else {
                                wptr = va_arg ( args, wchar_t * );
+                               if ( ! wptr )
+                                       ptr = "<NULL>";
                        }
-                       if ( ( ptr == NULL ) && ( wptr == NULL ) )
-                               ptr = "<NULL>";
                } else if ( *fmt == 'p' ) {
                        intptr_t ptrval;
 
index ad732a0d2979b212df3a938d2d0491ed331f315b..f388b3ded6349c15258b7bb4994cab3cf51e5664 100644 (file)
@@ -108,6 +108,10 @@ static void vsprintf_test_exec ( void ) {
        snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
        snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
                      0x1000000ULL, 0x3f000000ULL );
+
+       /* Null string (used for debug messages) */
+       snprintf_ok ( 16, "<NULL>", "%s", ( ( char * ) NULL ) );
+       snprintf_ok ( 16, "<NULL>", "%ls", ( ( wchar_t * ) NULL ) );
 }
 
 /** vsprintf() self-test */