1 --- gdtoa-gethex.c.orig 2010-02-24 20:50:10.000000000 -0800
2 +++ gdtoa-gethex.c 2010-02-24 21:26:32.000000000 -0800
3 @@ -29,34 +29,40 @@ THIS SOFTWARE.
4 /* Please send bug reports to David M. Gay (dmg at acm dot org,
5 * with " at " changed at "@" and " dot " changed to "."). */
7 +#include "xlocale_private.h"
11 +#include <sys/types.h>
19 -gethex(sp, fpi, exp, bp, sign)
20 - CONST char **sp; FPI *fpi; Long *exp; Bigint **bp; int sign;
21 +gethex(sp, fpi, exp, bp, sign, loc)
22 + CONST char **sp; FPI *fpi; Long *exp; Bigint **bp; int sign; locale_t loc;
24 -gethex( CONST char **sp, FPI *fpi, Long *exp, Bigint **bp, int sign)
25 +gethex( CONST char **sp, FPI *fpi, Long *exp, Bigint **bp, int sign, locale_t loc)
29 CONST unsigned char *decpt, *s0, *s, *s1;
30 + unsigned char *strunc;
31 int big, esign, havedig, irv, j, k, n, n0, nbits, up, zret;
32 ULong L, lostbits, *x;
36 + NORMALIZE_LOCALE(loc);
37 #ifdef NO_LOCALE_CACHE
38 - const unsigned char *decimalpoint = (unsigned char*)localeconv()->decimal_point;
39 + const unsigned char *decimalpoint = (unsigned char*)localeconv_l(loc)->decimal_point;
41 const unsigned char *decimalpoint;
42 static unsigned char *decimalpoint_cache;
43 if (!(s0 = decimalpoint_cache)) {
44 - s0 = (unsigned char*)localeconv()->decimal_point;
45 + s0 = (unsigned char*)localeconv_l(loc)->decimal_point;
46 if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) {
47 strcpy(decimalpoint_cache, s0);
48 s0 = decimalpoint_cache;
49 @@ -198,6 +204,57 @@ gethex( CONST char **sp, FPI *fpi, Long
51 return STRTOG_Normal | STRTOG_Inexlo;
54 + * Truncate the hex string if it is longer than the precision needed,
55 + * to avoid denial-of-service issues with very large strings. Use
56 + * additional digits to insure precision. Scan to-be-truncated digits
57 + * and replace with either '1' or '0' to ensure proper rounding.
60 + int maxdigits = ((fpi->nbits + 3) >> 2) + 2;
61 + size_t nd = s1 - s0;
63 + int dplen = strlen((const char *)decimalpoint);
68 + if (decpt && s0 < decpt)
70 + if (nd > maxdigits && (strunc = alloca(maxdigits + dplen + 2)) != NULL) {
71 + ssize_t nd0 = decpt ? decpt - s0 - dplen : nd;
72 + unsigned char *tp = strunc + maxdigits;
74 + if ((nd0 -= maxdigits) >= 0 || s0 >= decpt)
75 + memcpy(strunc, s0, maxdigits);
77 + memcpy(strunc, s0, maxdigits + dplen);
81 + e += (nd - (maxdigits + 1)) << 2;
90 + if (!found && decpt) {
97 + *tp++ = found ? '1' : '0';
105 for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)