changeset 9959:acbef346fd18

PSARC/2008/403 libc printf behaviour for NULL string 6724478 libc printf should not SEGV when passed NULL for %s format
author Darren Moffat <Darren.Moffat@Sun.COM>
date Wed, 24 Jun 2009 15:30:04 +0100
parents 108c82b6bad3
children 1c799f586b2f
files usr/src/lib/libc/port/print/doprnt.c
diffstat 1 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/lib/libc/port/print/doprnt.c	Wed Jun 24 12:00:21 2009 +0530
+++ b/usr/src/lib/libc/port/print/doprnt.c	Wed Jun 24 15:30:04 2009 +0100
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -52,6 +52,9 @@
 #include "mse.h"
 #include "xpg6.h"
 
+static const char nullstr[] = "(null)";
+static const wchar_t widenullstr[] = L"(null)";
+
 #if defined(__i386) || defined(__amd64) || defined(__sparcv9)
 #define	GETQVAL(arg)	(va_arg(arg, long double))
 #else /* !defined(__i386) && !defined(__sparcv9) */
@@ -1748,6 +1751,8 @@
 				lflag++;
 			}
 			bp = va_arg(args.ap, wchar_t *);
+			if (bp == NULL)
+				bp = (wchar_t *)widenullstr;
 			if (!(flagword & DOTSEEN)) {
 				/* wide character handling */
 				prec = MAXINT;
@@ -1770,6 +1775,8 @@
 			if (!wflag)
 				wflag++;
 			bp = va_arg(args.ap, char *);
+			if (bp == NULL)
+				bp = (char *)widenullstr;
 			if (!(flagword & DOTSEEN)) {
 				/* wide character handling */
 				prec = MAXINT;
@@ -1802,6 +1809,8 @@
 			}
 #ifdef	_WIDE
 			cbp = va_arg(args.ap, char *);
+			if (cbp == NULL)
+				cbp = (char *)nullstr;
 			if (!(flagword & DOTSEEN)) {
 				size_t	nwc;
 				wchar_t	*wstr;
@@ -1863,6 +1872,8 @@
 			wflag = 1;
 #else  /* _WIDE */
 			bp = va_arg(args.ap, char *);
+			if (bp == NULL)
+				bp = (char *)nullstr;
 			if (!(flagword & DOTSEEN)) {
 				if (wflag) {
 					/* wide character handling */