C99: Passing a null pointer as the buffer argument to snprintf() and

vsnprintf() is permitted when the size argument is zero as well; mostly
from Peter Seebach in PR standards/9603.
This commit is contained in:
kleink 2000-10-19 09:45:31 +00:00
parent d239df5374
commit 20b0e10891
3 changed files with 19 additions and 9 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: printf.3,v 1.13 1998/09/14 21:10:18 tv Exp $
.\" $NetBSD: printf.3,v 1.14 2000/10/19 09:45:31 kleink Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -149,6 +149,11 @@ if the return value is greater than or equal to the
.Fa size
argument, the string was too short
and some of the printed characters were discarded.
If
.Fa size
is zero, nothing is written and
.Fa str
may be a NULL pointer.
.Pp
.Fn sprintf
and

View File

@ -1,4 +1,4 @@
/* $NetBSD: snprintf.c,v 1.12 2000/01/22 22:19:19 mycroft Exp $ */
/* $NetBSD: snprintf.c,v 1.13 2000/10/19 09:45:31 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)snprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: snprintf.c,v 1.12 2000/01/22 22:19:19 mycroft Exp $");
__RCSID("$NetBSD: snprintf.c,v 1.13 2000/10/19 09:45:31 kleink Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -75,11 +75,13 @@ snprintf(str, n, fmt, va_alist)
va_list ap;
FILE f;
_DIAGASSERT(str != NULL);
_DIAGASSERT(n == 0 || str != NULL);
_DIAGASSERT(fmt != NULL);
if ((int)n < 1)
if ((int)n < 0) {
errno = EOVERFLOW;
return (-1);
}
#if __STDC__
va_start(ap, fmt);
#else

View File

@ -1,4 +1,4 @@
/* $NetBSD: vsnprintf.c,v 1.13 2000/01/22 22:19:19 mycroft Exp $ */
/* $NetBSD: vsnprintf.c,v 1.14 2000/10/19 09:45:31 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: vsnprintf.c,v 1.13 2000/01/22 22:19:19 mycroft Exp $");
__RCSID("$NetBSD: vsnprintf.c,v 1.14 2000/10/19 09:45:31 kleink Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -65,11 +65,14 @@ vsnprintf(str, n, fmt, ap)
int ret;
FILE f;
_DIAGASSERT(str != NULL);
_DIAGASSERT(n == 0 || str != NULL);
_DIAGASSERT(fmt != NULL);
if ((int)n < 1)
if ((int)n < 0) {
errno = EOVERFLOW;
return (-1);
}
f._file = -1;
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;