]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfscanf.c.patch
Libc-339.tar.gz
[apple/libc.git] / stdio / FreeBSD / vfscanf.c.patch
1 --- vfscanf.c.orig Tue Nov 18 16:48:06 2003
2 +++ vfscanf.c Tue Nov 18 17:35:55 2003
3 @@ -99,7 +99,9 @@
4 #define CT_FLOAT 4 /* %[efgEFG] conversion */
5
6 static const u_char *__sccl(char *, const u_char *);
7 -static int parsefloat(FILE *, char *, char *);
8 +#ifdef FLOATING_POINT
9 +static int parsefloat(FILE *, char **, size_t);
10 +#endif /* FLOATING_POINT */
11
12 int __scanfdebug = 0;
13
14 @@ -133,7 +135,6 @@
15 int flags; /* flags as defined above */
16 char *p0; /* saves original value of p when necessary */
17 int nassigned; /* number of fields assigned */
18 - int nconversions; /* number of conversions */
19 int nread; /* number of characters consumed from fp */
20 int base; /* base argument to conversion function */
21 char ccltab[256]; /* character class table for %[...] */
22 @@ -150,7 +151,6 @@
23 ORIENT(fp, -1);
24
25 nassigned = 0;
26 - nconversions = 0;
27 nread = 0;
28 for (;;) {
29 c = *fmt++;
30 @@ -288,7 +288,6 @@
31 break;
32
33 case 'n':
34 - nconversions++;
35 if (flags & SUPPRESS) /* ??? */
36 continue;
37 if (flags & SHORTSHORT)
38 @@ -421,7 +420,6 @@
39 nread += r;
40 nassigned++;
41 }
42 - nconversions++;
43 break;
44
45 case CT_CCL:
46 @@ -525,7 +523,6 @@
47 nassigned++;
48 }
49 nread += n;
50 - nconversions++;
51 break;
52
53 case CT_STRING:
54 @@ -607,7 +604,6 @@
55 nread += p - p0;
56 nassigned++;
57 }
58 - nconversions++;
59 continue;
60
61 case CT_INT:
62 @@ -758,39 +754,42 @@
63 nassigned++;
64 }
65 nread += p - buf;
66 - nconversions++;
67 break;
68
69 #ifdef FLOATING_POINT
70 case CT_FLOAT:
71 + {
72 + char *pbuf;
73 /* scan a floating point number as if by strtod */
74 - if (width == 0 || width > sizeof(buf) - 1)
75 - width = sizeof(buf) - 1;
76 - if ((width = parsefloat(fp, buf, buf + width)) == 0)
77 + if ((width = parsefloat(fp, &pbuf, width)) == 0) {
78 + if (pbuf)
79 + free(pbuf);
80 goto match_failure;
81 + }
82 if ((flags & SUPPRESS) == 0) {
83 if (flags & LONGDBL) {
84 - long double res = strtold(buf, &p);
85 + long double res = strtold(pbuf, &p);
86 *va_arg(ap, long double *) = res;
87 } else if (flags & LONG) {
88 - double res = strtod(buf, &p);
89 + double res = strtod(pbuf, &p);
90 *va_arg(ap, double *) = res;
91 } else {
92 - float res = strtof(buf, &p);
93 + float res = strtof(pbuf, &p);
94 *va_arg(ap, float *) = res;
95 }
96 - if (__scanfdebug && p - buf != width)
97 + if (__scanfdebug && p - pbuf != width)
98 abort();
99 nassigned++;
100 }
101 nread += width;
102 - nconversions++;
103 + free(pbuf);
104 break;
105 + }
106 #endif /* FLOATING_POINT */
107 }
108 }
109 input_failure:
110 - return (nconversions != 0 ? nassigned : EOF);
111 + return (nassigned ? nassigned : EOF);
112 match_failure:
113 return (nassigned);
114 }
115 @@ -910,7 +909,7 @@
116
117 #ifdef FLOATING_POINT
118 static int
119 -parsefloat(FILE *fp, char *buf, char *end)
120 +parsefloat(FILE *fp, char **buf, size_t width)
121 {
122 char *commit, *p;
123 int infnanpos = 0;
124 @@ -921,7 +920,16 @@
125 unsigned char c;
126 char decpt = *localeconv()->decimal_point;
127 _Bool gotmantdig = 0, ishex = 0;
128 + char *b, *e;
129 + size_t s;
130
131 + s = (width == 0 ? BUF : width + 1);
132 + b = (char *)malloc(s);
133 + if (b == NULL) {
134 + *buf = NULL;
135 + return 0;
136 + }
137 + e = b + (s - 1);
138 /*
139 * We set commit = p whenever the string we have read so far
140 * constitutes a valid representation of a floating point
141 @@ -931,8 +939,8 @@
142 * always necessary to read at least one character that doesn't
143 * match; thus, we can't short-circuit "infinity" or "nan(...)".
144 */
145 - commit = buf - 1;
146 - for (p = buf; p < end; ) {
147 + commit = b - 1;
148 + for (p = b; width == 0 || p < e; ) {
149 c = *fp->_p;
150 reswitch:
151 switch (state) {
152 @@ -1046,6 +1054,17 @@
153 default:
154 abort();
155 }
156 + if (p >= e) {
157 + size_t diff = (p - b);
158 + s += BUF;
159 + b = (char *)reallocf(b, s);
160 + if (b == NULL) {
161 + *buf = NULL;
162 + return 0;
163 + }
164 + e = b + (s - 1);
165 + p = b + diff;
166 + }
167 *p++ = c;
168 if (--fp->_r > 0)
169 fp->_p++;
170 @@ -1057,6 +1076,7 @@
171 while (commit < --p)
172 __ungetc(*(u_char *)p, fp);
173 *++commit = '\0';
174 - return (commit - buf);
175 + *buf = b;
176 + return (commit - b);
177 }
178 #endif