Add signal-safe versions of snprintf and vsnprintf

This commit is contained in:
christos 2006-10-27 19:59:58 +00:00
parent eb6da7fc35
commit abca035cf3
5 changed files with 204 additions and 14 deletions

View File

@ -1,5 +1,5 @@
# from: @(#)Makefile.inc 5.7 (Berkeley) 6/27/91
# $NetBSD: Makefile.inc,v 1.33 2006/02/16 23:26:19 christos Exp $
# $NetBSD: Makefile.inc,v 1.34 2006/10/27 19:59:58 christos Exp $
# stdio sources
.PATH: ${.CURDIR}/stdio
@ -15,11 +15,11 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c \
getc.c getchar.c gettemp.c getw.c getwc.c getwchar.c makebuf.c \
mkdtemp.c mkstemp.c perror.c printf.c putc.c putchar.c puts.c \
putw.c putwc.c putwchar.c refill.c remove.c rewind.c rget.c \
scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c sscanf.c \
stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \
scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c snprintf_ss.c \
sscanf.c stdio.c swprintf.c swscanf.c tmpfile.c ungetc.c ungetwc.c \
vasprintf.c vfprintf.c vfscanf.c vfwprintf.c vfwscanf.c vprintf.c \
vscanf.c vsnprintf.c vsscanf.c vswprintf.c vswscanf.c vwprintf.c \
vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
vscanf.c vsnprintf.c vsnprintf_ss.c vsscanf.c vswprintf.c vswscanf.c \
vwprintf.c vwscanf.c wbuf.c wprintf.c wscanf.c wsetup.c
.if !defined(AUDIT)
SRCS+= gets.c sprintf.c vsprintf.c tempnam.c tmpnam.c mktemp.c

View File

@ -1,4 +1,4 @@
/* $NetBSD: fvwrite.c,v 1.15 2003/08/07 16:43:26 agc Exp $ */
/* $NetBSD: fvwrite.c,v 1.16 2006/10/27 19:59:58 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)fvwrite.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: fvwrite.c,v 1.15 2003/08/07 16:43:26 agc Exp $");
__RCSID("$NetBSD: fvwrite.c,v 1.16 2006/10/27 19:59:58 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -56,6 +56,7 @@ __RCSID("$NetBSD: fvwrite.c,v 1.15 2003/08/07 16:43:26 agc Exp $");
* This routine is large and unsightly, but most of the ugliness due
* to the three different kinds of output buffering is handled here.
*/
/* NB: async-signal-safe when fp->_flags & __SAFE */
int
__sfvwrite(fp, uio)
FILE *fp;

View File

@ -0,0 +1,89 @@
/* $NetBSD: snprintf_ss.c,v 1.1 2006/10/27 19:59:58 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)snprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: snprintf_ss.c,v 1.1 2006/10/27 19:59:58 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include "reentrant.h"
#include "local.h"
#ifdef __weak_alias
__weak_alias(snprintf,_snprintf)
#endif
int
snprintf_ss(char *str, size_t n, char const *fmt, ...)
{
int ret;
va_list ap;
FILE f;
struct __sfileext fext;
unsigned char dummy[1];
_DIAGASSERT(n == 0 || str != NULL);
_DIAGASSERT(fmt != NULL);
if ((int)n < 0) {
errno = EINVAL;
return (-1);
}
va_start(ap, fmt);
_FILEEXT_SETUP(&f, &fext);
f._file = -1;
f._flags = __SWR | __SSTR | __SAFE;
if (n == 0) {
f._bf._base = f._p = dummy;
f._bf._size = f._w = 0;
} else {
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n - 1;
}
ret = __vfprintf_unlocked(&f, fmt, ap);
*f._p = 0;
va_end(ap);
return (ret);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: vfprintf.c,v 1.50 2006/02/16 23:26:19 christos Exp $ */
/* $NetBSD: vfprintf.c,v 1.51 2006/10/27 19:59:58 christos Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -37,7 +37,7 @@
#if 0
static char *sccsid = "@(#)vfprintf.c 5.50 (Berkeley) 12/16/92";
#else
__RCSID("$NetBSD: vfprintf.c,v 1.50 2006/02/16 23:26:19 christos Exp $");
__RCSID("$NetBSD: vfprintf.c,v 1.51 2006/10/27 19:59:58 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@ -73,6 +73,7 @@ static int __sbprintf __P((FILE *, const char *, va_list))
* Flush out all the vectors defined by the given uio,
* then reset it so that it can be reused.
*/
/* NB: async-signal-safe when fp->_flags & __SAFE */
static int
__sprint(fp, uio)
FILE *fp;
@ -192,6 +193,7 @@ vfprintf(fp, fmt0, ap)
/* NB: async-signal-safe when fp->_flags & __SAFE */
int
__vfprintf_unlocked(fp, fmt0, ap)
FILE *fp;
@ -335,11 +337,16 @@ __vfprintf_unlocked(fp, fmt0, ap)
*/
for (;;) {
cp = fmt;
while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
fmt += n;
if (wc == '%') {
fmt--;
break;
if (fp->_flags & __SAFE) {
for (; *fmt &&*fmt != '%'; fmt++)
continue;
} else {
while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
fmt += n;
if (wc == '%') {
fmt--;
break;
}
}
}
if ((m = fmt - cp) != 0) {
@ -501,6 +508,14 @@ reswitch: switch (ch) {
size = 3;
break;
}
if (fp->_flags & __SAFE) {
if (ch == 'E' || ch == 'F' || ch == 'G')
cp = "UNK";
else
cp = "unk";
size = 3;
break;
}
flags |= FPT;
if (dtoaresult)

View File

@ -0,0 +1,85 @@
/* $NetBSD: vsnprintf_ss.c,v 1.1 2006/10/27 19:59:58 christos Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Chris Torek.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)vsnprintf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: vsnprintf_ss.c,v 1.1 2006/10/27 19:59:58 christos Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include "reentrant.h"
#include "local.h"
#ifdef __weak_alias
__weak_alias(vsnprintf_ss,_vsnprintf_ss)
#endif
int
vsnprintf_ss(char *str, size_t n, const char *fmt, _BSD_VA_LIST_ ap)
{
int ret;
FILE f;
struct __sfileext fext;
unsigned char dummy[1];
_DIAGASSERT(n == 0 || str != NULL);
_DIAGASSERT(fmt != NULL);
if ((int)n < 0) {
errno = EINVAL;
return (-1);
}
_FILEEXT_SETUP(&f, &fext);
f._file = -1;
f._flags = __SWR | __SSTR | __SAFE;
if (n == 0) {
f._bf._base = f._p = dummy;
f._bf._size = f._w = 0;
} else {
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n - 1;
}
ret = __vfprintf_unlocked(&f, fmt, ap);
*f._p = 0;
return (ret);
}