From: Sascha Schumann Date: Tue, 7 May 2002 05:26:30 +0000 (+0000) Subject: Add thread-safety to some conversion functions and sync a bit with X-Git-Tag: php-4.3.0dev-ZendEngine2-Preview1~206 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=946fca9c91d7743441d7c46461fb6035168e2d44;p=php Add thread-safety to some conversion functions and sync a bit with APR. --- diff --git a/main/snprintf.c b/main/snprintf.c index f4cd19838d..bf14e904c5 100644 --- a/main/snprintf.c +++ b/main/snprintf.c @@ -88,7 +88,7 @@ * is declared as buf[ 100 ], buf_end should be &buf[ 100 ]) */ char * - ap_php_conv_10(register wide_int num, register bool_int is_unsigned, +ap_php_conv_10(register wide_int num, register bool_int is_unsigned, register bool_int * is_negative, char *buf_end, register int *len) { register char *p = buf_end; @@ -147,17 +147,19 @@ char * register char *s = buf; register char *p; int decimal_point; + char buf1[NDIG]; if (format == 'f') - p = ap_php_fcvt(num, precision, &decimal_point, is_negative); + p = ap_php_fcvt(num, precision, &decimal_point, is_negative, buf1); else /* either e or E format */ - p = ap_php_ecvt(num, precision + 1, &decimal_point, is_negative); + p = ap_php_ecvt(num, precision + 1, &decimal_point, is_negative, buf1); /* * Check for Infinity and NaN */ if (isalpha((int)*p)) { - *len = strlen(strcpy(buf, p)); + *len = strlen(p); + memcpy(buf, p, *len + 1); *is_negative = FALSE; return (buf); } @@ -274,12 +276,11 @@ char * #define NDIG 80 char * - ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag) +ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag, char *buf) { register int r2; double fi, fj; register char *p, *p1; - static char buf[NDIG]; if (ndigits >= NDIG - 1) ndigits = NDIG - 2; @@ -319,8 +320,7 @@ char * return (buf); } while (p <= p1 && p < &buf[NDIG]) { - arg *= 10; - arg = modf(arg, &fj); + arg = modf(arg * 10, &fj); *p++ = (int) fj + '0'; } if (p1 >= &buf[NDIG]) { @@ -348,15 +348,15 @@ char * } char * - ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign) +ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf) { - return (ap_php_cvt(arg, ndigits, decpt, sign, 1)); + return (ap_php_cvt(arg, ndigits, decpt, sign, 1, buf)); } char * - ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign) +ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf) { - return (ap_php_cvt(arg, ndigits, decpt, sign, 0)); + return (ap_php_cvt(arg, ndigits, decpt, sign, 0, buf)); } /* @@ -365,13 +365,14 @@ char * */ char * - ap_php_gcvt(double number, int ndigit, char *buf) +ap_php_gcvt(double number, int ndigit, char *buf, boolean_e altform) { int sign, decpt; register char *p1, *p2; register i; + char buf1[NDIG]; - p1 = ap_php_ecvt(number, ndigit, &decpt, &sign); + p1 = ap_php_ecvt(number, ndigit, &decpt, &sign, buf1); p2 = buf; if (sign) *p2++ = '-'; @@ -415,7 +416,7 @@ char * *p2++ = '.'; } } - if (p2[-1] == '.') + if (p2[-1] == '.' && !altform) p2--; *p2 = '\0'; return (buf); @@ -762,7 +763,8 @@ static int format_converter(register buffy * odp, const char *fmt, /* * * We use &num_buf[ 1 ], so that we have room for the sign */ - s = ap_php_gcvt(va_arg(ap, double), precision, &num_buf[1]); + s = ap_php_gcvt(va_arg(ap, double), precision, &num_buf[1], + alternate_form); if (*s == '-') prefix_char = *s++; else if (print_sign) diff --git a/main/snprintf.h b/main/snprintf.h index d05d8646d0..1efa77f122 100644 --- a/main/snprintf.h +++ b/main/snprintf.h @@ -34,6 +34,10 @@ int php_sprintf (char* s, const char* format, ...); #define sprintf php_sprintf #endif +typedef enum { + NO = 0, YES = 1 +} boolean_e; + #ifdef HAVE_GCVT #define ap_php_ecvt ecvt @@ -42,17 +46,13 @@ int php_sprintf (char* s, const char* format, ...); #else -extern char * ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag); -extern char * ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign); -extern char * ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign); -extern char * ap_php_gcvt(double number, int ndigit, char *buf); +extern char * ap_php_cvt(double arg, int ndigits, int *decpt, int *sign, int eflag, char *buf); +extern char * ap_php_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf); +extern char * ap_php_fcvt(double arg, int ndigits, int *decpt, int *sign, char *buf); +extern char * ap_php_gcvt(double number, int ndigit, char *buf, boolean_e altform); #endif -typedef enum { - NO = 0, YES = 1 -} boolean_e; - #define WIDE_INT long typedef WIDE_INT wide_int; typedef unsigned WIDE_INT u_wide_int;