Ver código fonte

[libc] Print "<NULL>" for wide-character NULL strings

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>
tags/v1.20.1
Michael Brown 8 anos atrás
pai
commit
cc8824ad4e
2 arquivos alterados com 8 adições e 2 exclusões
  1. 4
    2
      src/core/vsprintf.c
  2. 4
    0
      src/tests/vsprintf_test.c

+ 4
- 2
src/core/vsprintf.c Ver arquivo

@@ -257,11 +257,13 @@ size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
257 257
 		} else if ( *fmt == 's' ) {
258 258
 			if ( length < &type_sizes[LONG_LEN] ) {
259 259
 				ptr = va_arg ( args, char * );
260
+				if ( ! ptr )
261
+					ptr = "<NULL>";
260 262
 			} else {
261 263
 				wptr = va_arg ( args, wchar_t * );
264
+				if ( ! wptr )
265
+					ptr = "<NULL>";
262 266
 			}
263
-			if ( ( ptr == NULL ) && ( wptr == NULL ) )
264
-				ptr = "<NULL>";
265 267
 		} else if ( *fmt == 'p' ) {
266 268
 			intptr_t ptrval;
267 269
 

+ 4
- 0
src/tests/vsprintf_test.c Ver arquivo

@@ -108,6 +108,10 @@ static void vsprintf_test_exec ( void ) {
108 108
 	snprintf_ok ( 64, "PCI 00:1f.3", "PCI %02x:%02x.%x", 0x00, 0x1f, 0x03 );
109 109
 	snprintf_ok ( 64, "Region [1000000,3f000000)", "Region [%llx,%llx)",
110 110
 		      0x1000000ULL, 0x3f000000ULL );
111
+
112
+	/* Null string (used for debug messages) */
113
+	snprintf_ok ( 16, "<NULL>", "%s", ( ( char * ) NULL ) );
114
+	snprintf_ok ( 16, "<NULL>", "%ls", ( ( wchar_t * ) NULL ) );
111 115
 }
112 116
 
113 117
 /** vsprintf() self-test */

Carregando…
Cancelar
Salvar