- Use an initializer macro to initialize fields in 'fake' FILE objects used

by *sprintf(), etc.
- Explicitly initialize _fl_mutex to PTHREAD_MUTEX_INITIALIZER for all FILE
  objects.  This is currently a nop on FreeBSD, but is import for other
  platforms (or in the future) where PTHREAD_MUTEX_INITIALIZER is not simply
  zero.

PR:		threads/141198
Reported by:	Jeremy Huddleston @ Apple
MFC after:	2 weeks
This commit is contained in:
John Baldwin 2010-03-11 17:03:32 +00:00
parent f458b72661
commit 1b0181df2f
12 changed files with 22 additions and 39 deletions

View File

@ -61,6 +61,7 @@ int __sdidinit;
._read = __sread, \
._seek = __sseek, \
._write = __swrite, \
._fl_mutex = PTHREAD_MUTEX_INITIALIZER, \
}
/* the usual - (stdin + stdout + stderr) */
static FILE usual[FOPEN_MAX - 3];
@ -96,7 +97,7 @@ moreglue(n)
int n;
{
struct glue *g;
static FILE empty;
static FILE empty = { ._fl_mutex = PTHREAD_MUTEX_INITIALIZER };
FILE *p;
g = (struct glue *)malloc(sizeof(*g) + ALIGNBYTES + n * sizeof(FILE));
@ -154,7 +155,7 @@ found:
fp->_ub._size = 0;
fp->_lb._base = NULL; /* no line buffer */
fp->_lb._size = 0;
/* fp->_lock = NULL; */ /* once set always set (reused) */
/* fp->_fl_mutex = NULL; */ /* once set always set (reused) */
fp->_orientation = 0;
memset(&fp->_mbstate, 0, sizeof(mbstate_t));
return (fp);

View File

@ -109,6 +109,14 @@ extern int __sdidinit;
(fp)->_lb._base = NULL; \
}
/*
* Structure initializations for 'fake' FILE objects.
*/
#define FAKE_FILE { \
._file = -1, \
._fl_mutex = PTHREAD_MUTEX_INITIALIZER, \
}
/*
* Set the orientation for a stream. If o > 0, the stream has wide-
* orientation. If o < 0, the stream has byte-orientation.

View File

@ -48,7 +48,7 @@ snprintf(char * __restrict str, size_t n, char const * __restrict fmt, ...)
size_t on;
int ret;
va_list ap;
FILE f;
FILE f = FAKE_FILE;
on = n;
if (n != 0)
@ -56,12 +56,9 @@ snprintf(char * __restrict str, size_t n, char const * __restrict fmt, ...)
if (n > INT_MAX)
n = INT_MAX;
va_start(ap, fmt);
f._file = -1;
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n;
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
ret = __vfprintf(&f, fmt, ap);
if (on > 0)
*f._p = '\0';

View File

@ -42,9 +42,8 @@ vasprintf(str, fmt, ap)
__va_list ap;
{
int ret;
FILE f;
FILE f = FAKE_FILE;
f._file = -1;
f._flags = __SWR | __SSTR | __SALC;
f._bf._base = f._p = (unsigned char *)malloc(128);
if (f._bf._base == NULL) {
@ -53,8 +52,6 @@ vasprintf(str, fmt, ap)
return (-1);
}
f._bf._size = f._w = 127; /* Leave room for the NUL */
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
ret = __vfprintf(&f, fmt, ap);
if (ret < 0) {
free(f._bf._base);

View File

@ -39,7 +39,7 @@ __FBSDID("$FreeBSD$");
int
vdprintf(int fd, const char * __restrict fmt, va_list ap)
{
FILE f;
FILE f = FAKE_FILE;
unsigned char buf[BUFSIZ];
int ret;
@ -56,8 +56,6 @@ vdprintf(int fd, const char * __restrict fmt, va_list ap)
f._write = __swrite;
f._bf._base = buf;
f._bf._size = sizeof(buf);
f._orientation = 0;
bzero(&f._mbstate, sizeof(f._mbstate));
if ((ret = __vfprintf(&f, fmt, ap)) < 0)
return (ret);

View File

@ -169,7 +169,7 @@ static int
__sbprintf(FILE *fp, const char *fmt, va_list ap)
{
int ret;
FILE fake;
FILE fake = FAKE_FILE;
unsigned char buf[BUFSIZ];
/* XXX This is probably not needed. */

View File

@ -47,7 +47,7 @@ vsnprintf(char * __restrict str, size_t n, const char * __restrict fmt,
size_t on;
int ret;
char dummy[2];
FILE f;
FILE f = FAKE_FILE;
on = n;
if (n != 0)
@ -61,12 +61,9 @@ vsnprintf(char * __restrict str, size_t n, const char * __restrict fmt,
str = dummy;
n = 1;
}
f._file = -1;
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = n;
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
ret = __vfprintf(&f, fmt, ap);
if (on > 0)
*f._p = '\0';

View File

@ -44,14 +44,11 @@ int
vsprintf(char * __restrict str, const char * __restrict fmt, __va_list ap)
{
int ret;
FILE f;
FILE f = FAKE_FILE;
f._file = -1;
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._w = INT_MAX;
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
ret = __vfprintf(&f, fmt, ap);
*f._p = 0;
return (ret);

View File

@ -55,16 +55,11 @@ int
vsscanf(const char * __restrict str, const char * __restrict fmt,
__va_list ap)
{
FILE f;
FILE f = FAKE_FILE;
f._file = -1;
f._flags = __SRD;
f._bf._base = f._p = (unsigned char *)str;
f._bf._size = f._r = strlen(str);
f._read = eofread;
f._ub._base = NULL;
f._lb._base = NULL;
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
return (__svfscanf(&f, fmt, ap));
}

View File

@ -45,7 +45,7 @@ vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
{
static const mbstate_t initial;
mbstate_t mbs;
FILE f;
FILE f = FAKE_FILE;
char *mbp;
int ret, sverrno;
size_t nwc;
@ -55,7 +55,6 @@ vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
return (-1);
}
f._file = -1;
f._flags = __SWR | __SSTR | __SALC;
f._bf._base = f._p = (unsigned char *)malloc(128);
if (f._bf._base == NULL) {
@ -63,8 +62,6 @@ vswprintf(wchar_t * __restrict s, size_t n, const wchar_t * __restrict fmt,
return (-1);
}
f._bf._size = f._w = 127; /* Leave room for the NUL */
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
ret = __vfwprintf(&f, fmt, ap);
if (ret < 0) {
sverrno = errno;

View File

@ -62,7 +62,7 @@ vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
{
static const mbstate_t initial;
mbstate_t mbs;
FILE f;
FILE f = FAKE_FILE;
char *mbstr;
size_t mlen;
int r;
@ -80,15 +80,10 @@ vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict fmt,
free(mbstr);
return (EOF);
}
f._file = -1;
f._flags = __SRD;
f._bf._base = f._p = (unsigned char *)mbstr;
f._bf._size = f._r = mlen;
f._read = eofread;
f._ub._base = NULL;
f._lb._base = NULL;
f._orientation = 0;
memset(&f._mbstate, 0, sizeof(mbstate_t));
r = __vfwscanf(&f, fmt, ap);
free(mbstr);

View File

@ -48,6 +48,7 @@
#include <wchar.h>
#include "un-namespace.h"
#include "local.h"
#include "printf.h"
#include "fvwrite.h"
@ -575,7 +576,7 @@ static int
__v3printf(FILE *fp, const char *fmt, int pct, va_list ap)
{
int ret;
FILE fake;
FILE fake = FAKE_FILE;
unsigned char buf[BUFSIZ];
/* copy the important variables */