Loading...
stdio/FreeBSD/vfscanf.c.patch Libc-498 Libc-763.13
--- Libc/Libc-498/stdio/FreeBSD/vfscanf.c.patch
+++ Libc/Libc-763.13/stdio/FreeBSD/vfscanf.c.patch
@@ -1,48 +1,45 @@
---- vfscanf.c.orig	2004-11-25 11:38:35.000000000 -0800
-+++ vfscanf.c	2005-05-20 00:46:37.000000000 -0700
-@@ -40,6 +40,8 @@
+--- vfscanf.c.orig	2010-07-15 10:03:36.000000000 -0700
++++ vfscanf.c	2010-07-15 10:18:58.000000000 -0700
+@@ -36,6 +36,8 @@ static char sccsid[] = "@(#)vfscanf.c	8.
  #include <sys/cdefs.h>
- __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.37 2004/05/02 10:55:05 das Exp $");
+ __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.43 2009/01/19 06:19:51 das Exp $");
  
 +#include "xlocale_private.h"
 +
  #include "namespace.h"
  #include <ctype.h>
  #include <inttypes.h>
-@@ -97,10 +99,21 @@
+@@ -46,6 +48,7 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/v
+ #include <string.h>
+ #include <wchar.h>
+ #include <wctype.h>
++#include <pthread.h>
+ #include "un-namespace.h"
+ 
+ #include "collate.h"
+@@ -93,9 +96,9 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/v
  #define	CT_INT		3	/* %[dioupxX] conversion */
  #define	CT_FLOAT	4	/* %[efgEFG] conversion */
  
 -static const u_char *__sccl(char *, const u_char *);
++static const u_char *__sccl(char *, const u_char *, locale_t);
+ #ifndef NO_FLOATING_POINT
 -static int parsefloat(FILE *, char *, char *);
-+static const u_char *__sccl(char *, const u_char *, locale_t);
-+#ifndef NO_FLOATING_POINT
 +static int parsefloat(FILE *, char **, size_t, locale_t);
-+#endif /* !NO_FLOATING_POINT */
- 
-+/*
-+ * For ppc, we need to have the 64-bit long double version defining storage for
-+ * __scanfdebug, to be compatible with 10.3.  For ppc64 and i386, we want the
-+ * storage defined in the only version.
-+ */
-+#if defined(__ppc__) && !defined(BUILDING_VARIANT)
-+extern int __scanfdebug;
-+#else /* !__ppc__ || BUILDING_VARIANT */
- int __scanfdebug = 0;
-+#endif /* __ppc__ && !BUILDING_VARIANT */
+ #endif
  
  __weak_reference(__vfscanf, vfscanf);
- 
-@@ -108,12 +121,24 @@
+@@ -104,12 +107,24 @@ __weak_reference(__vfscanf, vfscanf);
   * __vfscanf - MT-safe version
   */
  int
 -__vfscanf(FILE *fp, char const *fmt0, va_list ap)
 +__vfscanf(FILE * __restrict fp, char const * __restrict fmt0, va_list ap)
-+{
-+	int ret;
-+
-+	FLOCKFILE(fp);
+ {
+ 	int ret;
+ 
+ 	FLOCKFILE(fp);
+-	ret = __svfscanf(fp, fmt0, ap);
 +	ret = __svfscanf_l(fp, __current_locale(), fmt0, ap);
 +	FUNLOCKFILE(fp);
 +	return (ret);
@@ -50,17 +47,16 @@
 +
 +int
 +vfscanf_l(FILE * __restrict fp, locale_t loc, char const * __restrict fmt0, va_list ap)
- {
- 	int ret;
- 
++{
++	int ret;
++
 +	NORMALIZE_LOCALE(loc);
- 	FLOCKFILE(fp);
--	ret = __svfscanf(fp, fmt0, ap);
++	FLOCKFILE(fp);
 +	ret = __svfscanf_l(fp, loc, fmt0, ap);
  	FUNLOCKFILE(fp);
  	return (ret);
  }
-@@ -121,8 +146,8 @@
+@@ -117,8 +132,8 @@ __vfscanf(FILE *fp, char const *fmt0, va
  /*
   * __svfscanf - non-MT-safe version of __vfscanf
   */
@@ -71,7 +67,7 @@
  {
  	const u_char *fmt = (const u_char *)fmt0;
  	int c;			/* character from format, or conversion */
-@@ -132,7 +157,6 @@
+@@ -128,36 +143,43 @@ __svfscanf(FILE *fp, const char *fmt0, v
  	int flags;		/* flags as defined above */
  	char *p0;		/* saves original value of p when necessary */
  	int nassigned;		/* number of fields assigned */
@@ -79,9 +75,8 @@
  	int nread;		/* number of characters consumed from fp */
  	int base;		/* base argument to conversion function */
  	char ccltab[256];	/* character class table for %[...] */
-@@ -140,24 +164,29 @@
+ 	char buf[BUF];		/* buffer for numeric and mb conversions */
  	wchar_t *wcp;		/* handy wide character pointer */
- 	wchar_t *wcp0;		/* saves original value of wcp */
  	size_t nconv;		/* length of multibyte sequence converted */
 +	int index;		/* %index$, zero if unset */
 +	va_list ap_orig;	/* to reset ap to first argument */
@@ -112,7 +107,35 @@
  				nread++, fp->_r--, fp->_p++;
  			continue;
  		}
-@@ -181,6 +210,18 @@
+-		if (c != '%')
++		if (c != '%') {
++			if (fp->_r <= 0 && __srefill(fp))
++				goto input_failure;
+ 			goto literal;
++		}
+ 		width = 0;
+ 		flags = 0;
+ 		/*
+@@ -167,15 +189,35 @@ __svfscanf(FILE *fp, const char *fmt0, v
+ again:		c = *fmt++;
+ 		switch (c) {
+ 		case '%':
++			/* Consume leading white space */
++			for(;;) {
++				if (fp->_r <= 0 && __srefill(fp))
++					goto input_failure;
++				if (!isspace_l(*fp->_p, loc))
++					break;
++				nread++;
++				fp->_r--;
++				fp->_p++;
++			}
+ literal:
+-			if (fp->_r <= 0 && __srefill(fp))
+-				goto input_failure;
+ 			if (*fp->_p != c)
+ 				goto match_failure;
+ 			fp->_r--, fp->_p++;
  			nread++;
  			continue;
  
@@ -131,7 +154,7 @@
  		case '*':
  			flags |= SUPPRESS;
  			goto again;
-@@ -267,7 +308,7 @@
+@@ -262,7 +304,7 @@ literal:
  			break;
  
  		case '[':
@@ -140,15 +163,48 @@
  			flags |= NOSKIP;
  			c = CT_CCL;
  			break;
-@@ -288,7 +329,6 @@
+@@ -283,27 +325,28 @@ literal:
  			break;
  
  		case 'n':
 -			nconversions++;
- 			if (flags & SUPPRESS)	/* ??? */
+-			if (flags & SUPPRESS)	/* ??? */
++		{
++			void *ptr = va_arg(ap, void *);
++			if ((ptr == NULL) || (flags & SUPPRESS))	/* ??? */
  				continue;
- 			if (flags & SHORTSHORT)
-@@ -330,7 +370,7 @@
+-			if (flags & SHORTSHORT)
+-				*va_arg(ap, char *) = nread;
++			else if (flags & SHORTSHORT)
++				*(char *)ptr = nread;
+ 			else if (flags & SHORT)
+-				*va_arg(ap, short *) = nread;
++				*(short *)ptr = nread;
+ 			else if (flags & LONG)
+-				*va_arg(ap, long *) = nread;
++				*(long *)ptr = nread;
+ 			else if (flags & LONGLONG)
+-				*va_arg(ap, long long *) = nread;
++				*(long long *)ptr = nread;
+ 			else if (flags & INTMAXT)
+-				*va_arg(ap, intmax_t *) = nread;
++				*(intmax_t *)ptr = nread;
+ 			else if (flags & SIZET)
+-				*va_arg(ap, size_t *) = nread;
++				*(size_t *)ptr = nread;
+ 			else if (flags & PTRDIFFT)
+-				*va_arg(ap, ptrdiff_t *) = nread;
++				*(ptrdiff_t *)ptr = nread;
+ 			else
+-				*va_arg(ap, int *) = nread;
++				*(int *)ptr = nread;
+ 			continue;
+-
++		}
+ 		default:
+ 			goto match_failure;
+ 
+@@ -325,7 +368,7 @@ literal:
  		 * that suppress this.
  		 */
  		if ((flags & NOSKIP) == 0) {
@@ -157,7 +213,7 @@
  				nread++;
  				if (--fp->_r > 0)
  					fp->_p++;
-@@ -360,7 +400,7 @@
+@@ -355,7 +398,7 @@ literal:
  					wcp = NULL;
  				n = 0;
  				while (width != 0) {
@@ -166,7 +222,7 @@
  						fp->_flags |= __SERR;
  						goto input_failure;
  					}
-@@ -368,7 +408,7 @@
+@@ -363,7 +406,7 @@ literal:
  					fp->_p++;
  					fp->_r--;
  					mbs = initial;
@@ -175,7 +231,7 @@
  					if (nconv == (size_t)-1) {
  						fp->_flags |= __SERR;
  						goto input_failure;
-@@ -421,7 +461,6 @@
+@@ -416,7 +459,6 @@ literal:
  				nread += r;
  				nassigned++;
  			}
@@ -183,7 +239,7 @@
  			break;
  
  		case CT_CCL:
-@@ -440,7 +479,7 @@
+@@ -435,7 +477,7 @@ literal:
  				n = 0;
  				nchars = 0;
  				while (width != 0) {
@@ -192,7 +248,7 @@
  						fp->_flags |= __SERR;
  						goto input_failure;
  					}
-@@ -448,7 +487,7 @@
+@@ -443,7 +485,7 @@ literal:
  					fp->_p++;
  					fp->_r--;
  					mbs = initial;
@@ -201,7 +257,7 @@
  					if (nconv == (size_t)-1) {
  						fp->_flags |= __SERR;
  						goto input_failure;
-@@ -456,8 +495,8 @@
+@@ -451,8 +493,8 @@ literal:
  					if (nconv == 0)
  						*wcp = L'\0';
  					if (nconv != (size_t)-2) {
@@ -212,7 +268,7 @@
  							while (n != 0) {
  								n--;
  								__ungetc(buf[n],
-@@ -525,7 +564,6 @@
+@@ -520,7 +562,6 @@ literal:
  				nassigned++;
  			}
  			nread += n;
@@ -220,7 +276,7 @@
  			break;
  
  		case CT_STRING:
-@@ -540,8 +578,8 @@
+@@ -535,8 +576,8 @@ literal:
  				else
  					wcp = &twc;
  				n = 0;
@@ -231,7 +287,7 @@
  						fp->_flags |= __SERR;
  						goto input_failure;
  					}
-@@ -549,7 +587,7 @@
+@@ -544,7 +585,7 @@ literal:
  					fp->_p++;
  					fp->_r--;
  					mbs = initial;
@@ -240,7 +296,7 @@
  					if (nconv == (size_t)-1) {
  						fp->_flags |= __SERR;
  						goto input_failure;
-@@ -557,7 +595,7 @@
+@@ -552,7 +593,7 @@ literal:
  					if (nconv == 0)
  						*wcp = L'\0';
  					if (nconv != (size_t)-2) {
@@ -249,7 +305,7 @@
  							while (n != 0) {
  								n--;
  								__ungetc(buf[n],
-@@ -585,7 +623,7 @@
+@@ -580,7 +621,7 @@ literal:
  				}
  			} else if (flags & SUPPRESS) {
  				n = 0;
@@ -258,7 +314,7 @@
  					n++, fp->_r--, fp->_p++;
  					if (--width == 0)
  						break;
-@@ -595,7 +633,7 @@
+@@ -590,7 +631,7 @@ literal:
  				nread += n;
  			} else {
  				p0 = p = va_arg(ap, char *);
@@ -267,7 +323,7 @@
  					fp->_r--;
  					*p++ = *fp->_p++;
  					if (--width == 0)
-@@ -607,7 +645,6 @@
+@@ -602,7 +643,6 @@ literal:
  				nread += p - p0;
  				nassigned++;
  			}
@@ -275,7 +331,7 @@
  			continue;
  
  		case CT_INT:
-@@ -738,9 +775,9 @@
+@@ -733,9 +773,9 @@ literal:
  
  				*p = 0;
  				if ((flags & UNSIGNED) == 0)
@@ -287,7 +343,7 @@
  				if (flags & POINTER)
  					*va_arg(ap, void **) =
  							(void *)(uintptr_t)res;
-@@ -763,43 +800,48 @@
+@@ -758,41 +798,46 @@ literal:
  				nassigned++;
  			}
  			nread += p - buf;
@@ -318,9 +374,6 @@
 +					float res = strtof_l(pbuf, &p, loc);
  					*va_arg(ap, float *) = res;
  				}
--				if (__scanfdebug && p - buf != width)
-+				if (__scanfdebug && p - pbuf != width)
- 					abort();
  				nassigned++;
  			}
  			nread += width;
@@ -346,7 +399,7 @@
  /*
   * Fill in the given table from the scanset at the given format
   * (just after `[').  Return a pointer to the character past the
-@@ -807,9 +849,10 @@
+@@ -800,9 +845,10 @@ match_failure:
   * considered part of the scanset.
   */
  static const u_char *
@@ -358,7 +411,7 @@
  {
  	int c, n, v, i;
  
-@@ -845,6 +888,7 @@
+@@ -838,6 +884,7 @@ doswitch:
  			return (fmt - 1);
  
  		case '-':
@@ -366,7 +419,7 @@
  			/*
  			 * A scanset of the form
  			 *	[01+-]
-@@ -865,8 +909,8 @@
+@@ -858,8 +905,8 @@ doswitch:
  			 */
  			n = *fmt;
  			if (n == ']'
@@ -377,7 +430,7 @@
  			       )
  			   ) {
  				c = '-';
-@@ -874,14 +918,14 @@
+@@ -867,14 +914,14 @@ doswitch:
  			}
  			fmt++;
  			/* fill in the range */
@@ -395,7 +448,7 @@
  					   )
  						tab[i] = v;
  			}
-@@ -901,7 +945,7 @@
+@@ -894,7 +941,7 @@ doswitch:
  				return (fmt);
  #endif
  			break;
@@ -404,54 +457,84 @@
  		case ']':		/* end of scanset */
  			return (fmt);
  
-@@ -915,18 +959,42 @@
+@@ -907,8 +954,54 @@ doswitch:
+ }
  
  #ifndef NO_FLOATING_POINT
++/*
++ * Maintain a per-thread parsefloat buffer, shared by __svfscanf_l and
++ * __vfwscanf.
++ */
++#ifdef BUILDING_VARIANT
++extern char *__parsefloat_buf(size_t s);
++#else /* !BUILDING_VARIANT */
++__private_extern__ char *
++__parsefloat_buf(size_t s)
++{
++	char *b;
++	static pthread_key_t    parsefloat_tsd_key = (pthread_key_t)-1;
++	static pthread_mutex_t  parsefloat_tsd_lock = PTHREAD_MUTEX_INITIALIZER;
++	static size_t bsiz = 0;
++
++	if (parsefloat_tsd_key == (pthread_key_t)-1) {
++		pthread_mutex_lock(&parsefloat_tsd_lock);
++		if (parsefloat_tsd_key == (pthread_key_t)-1) {
++			parsefloat_tsd_key = __LIBC_PTHREAD_KEY_PARSEFLOAT;
++			pthread_key_init_np(parsefloat_tsd_key, free);
++		}
++		pthread_mutex_unlock(&parsefloat_tsd_lock);
++	}
++	if ((b = (char *)pthread_getspecific(parsefloat_tsd_key)) == NULL) {
++		bsiz = s > BUF ? s : BUF;
++		b = (char *)malloc(bsiz);
++		if (b == NULL) {
++			bsiz = 0;
++			return NULL;
++		}
++		pthread_setspecific(parsefloat_tsd_key, b);
++		return b;
++	}
++	if (s > bsiz) {
++		b = (char *)reallocf(b, s);
++		pthread_setspecific(parsefloat_tsd_key, b);
++		if (b == NULL) {
++			bsiz = 0;
++			return NULL;
++		}
++		bsiz = s;
++	}
++	return b;
++}
++#endif /* BUILDING_VARIANT */
++
  static int
 -parsefloat(FILE *fp, char *buf, char *end)
 +parsefloat(FILE *fp, char **buf, size_t width, locale_t loc)
  {
  	char *commit, *p;
- 	int infnanpos = 0;
- 	enum {
- 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
--		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
-+		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS, S_DECIMAL_POINT
+ 	int infnanpos = 0, decptpos = 0;
+@@ -917,9 +1010,18 @@ parsefloat(FILE *fp, char *buf, char *en
+ 		S_DIGITS, S_DECPT, S_FRAC, S_EXP, S_EXPDIGITS
  	} state = S_START;
  	unsigned char c;
--	char decpt = *localeconv()->decimal_point;
-+	unsigned char *decpt = (unsigned char *)localeconv_l(loc)->decimal_point;
-+	char *decpt_start;
+-	const char *decpt = localeconv()->decimal_point;
++	const char *decpt = localeconv_l(loc)->decimal_point;
  	_Bool gotmantdig = 0, ishex = 0;
 -
-+	static char *b = NULL;
-+	static size_t bsiz = 0;
++	char *b;
 +	char *e;
 +	size_t s;
 +
-+	if (bsiz = 0) {
-+		b = (char *)malloc(BUF);
-+		if (b == NULL) {
-+			*buf = NULL;
-+			return 0;
-+		}
-+		bsiz = BUF;
-+	}
 +	s = (width == 0 ? BUF : (width + 1));
-+	if (s > bsiz) {
-+		b = (char *)reallocf(b, s);
-+		if (b == NULL) {
-+			bsiz = 0;
-+			*buf = NULL;
-+			return 0;
-+		}
-+		bsiz = s;
++	if ((b = __parsefloat_buf(s)) == NULL) {
++		*buf = NULL;
++		return 0;
 +	}
 +	e = b + (s - 1);
  	/*
  	 * We set commit = p whenever the string we have read so far
  	 * constitutes a valid representation of a floating point
-@@ -936,8 +1004,8 @@
+@@ -929,8 +1031,8 @@ parsefloat(FILE *fp, char *buf, char *en
  	 * always necessary to read at least one character that doesn't
  	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
  	 */
@@ -462,54 +545,25 @@
  		c = *fp->_p;
  reswitch:
  		switch (state) {
-@@ -997,7 +1065,7 @@
+@@ -988,7 +1090,7 @@ reswitch:
  				if (c == ')') {
  					commit = p;
- 					infnanpos = -2;
+ 					state = S_DONE;
 -				} else if (!isalnum(c) && c != '_')
 +				} else if (!isalnum_l(c, loc) && c != '_')
  					goto parsedone;
  				break;
  			}
-@@ -1013,16 +1081,33 @@
+@@ -1006,7 +1108,7 @@ reswitch:
  				goto reswitch;
  			}
  		case S_DIGITS:
--			if ((ishex && isxdigit(c)) || isdigit(c))
-+			if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc))
+-			if ((ishex && isxdigit(c)) || isdigit(c)) {
++			if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc)) {
  				gotmantdig = 1;
- 			else {
--				state = S_FRAC;
--				if (c != decpt)
--					goto reswitch;
-+				state = S_DECIMAL_POINT;
-+				decpt_start = p;
-+				goto reswitch;
- 			}
- 			if (gotmantdig)
  				commit = p;
- 			break;
-+		case S_DECIMAL_POINT:
-+			if (*decpt == 0) {
-+				if (gotmantdig)
-+					commit = p - 1;
-+				state = S_FRAC;
-+				goto reswitch;
-+			}
-+			if (*decpt++ == c)
-+				break;
-+			/* not decimal point */
-+			state = S_FRAC;
-+			if (decpt_start == p)
-+				goto reswitch;
-+			while (decpt_start < --p)
-+				__ungetc(*(u_char *)p, fp);
-+			c = *(u_char *)p;
-+			goto reswitch;
- 		case S_FRAC:
- 			if (((c == 'E' || c == 'e') && !ishex) ||
- 			    ((c == 'P' || c == 'p') && ishex)) {
-@@ -1030,7 +1115,7 @@
+ 				break;
+@@ -1041,7 +1143,7 @@ reswitch:
  					goto parsedone;
  				else
  					state = S_EXP;
@@ -518,7 +572,7 @@
  				commit = p;
  				gotmantdig = 1;
  			} else
-@@ -1043,7 +1128,7 @@
+@@ -1054,13 +1156,26 @@ reswitch:
  			else
  				goto reswitch;
  		case S_EXPDIGITS:
@@ -527,29 +581,27 @@
  				commit = p;
  			else
  				goto parsedone;
-@@ -1051,6 +1136,21 @@
+ 			break;
  		default:
- 			abort();
- 		}
+-			abort();
++			LIBC_ABORT("unknown state %d", state);
++		}
 +		if (p >= e) {
 +			ssize_t diff = (p - b);
 +			ssize_t com = (commit - b);
 +			s += BUF;
-+			b = (char *)reallocf(b, s);
++			b = __parsefloat_buf(s);
 +			if (b == NULL) {
-+				bsiz = 0;
 +				*buf = NULL;
 +				return 0;
 +			}
-+			bsiz = s;
 +			e = b + (s - 1);
 +			p = b + diff;
 +			commit = b + com;
-+		}
+ 		}
  		*p++ = c;
  		if (--fp->_r > 0)
- 			fp->_p++;
-@@ -1062,6 +1162,7 @@
+@@ -1073,6 +1188,7 @@ parsedone:
  	while (commit < --p)
  		__ungetc(*(u_char *)p, fp);
  	*++commit = '\0';