Skip to content

Commit 3e62aae

Browse files
committed
Fix bug #62112: number_format() is not binary safe
The bug report actually urges PHP 5.3's behavior to be reinstated -- that is, make "\0", when used as a separator, be the same as no separator at all. I believe that is not a proper course of action and that "\0" being interpreted as no seperator was a bug in PHP 5.3. Using "" for no separator, in both 5.3 and 5.4, before and after this change, causes no separator to be used, so there is no functionality loss.
1 parent a07d76c commit 3e62aae

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PHP NEWS
1717

1818
- Core:
1919
. Fixed missing bound check in iptcparse(). (chris at chiappa.net)
20+
. Fixed bug #62112 (number_format() is not binary safe). (Gustavo)
2021
. Fixed bug #62005 (unexpected behavior when incrementally assigning to a
2122
member of a null object). (Laruence)
2223
. Fixed bug #61998 (Using traits with method aliases appears to result in

ext/standard/math.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,9 @@ PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char tho
10971097
return _php_math_number_format_ex(d, dec, &dec_point, 1, &thousand_sep, 1);
10981098
}
10991099

1100-
PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size_t dec_point_len, char *thousand_sep, size_t thousand_sep_len)
1100+
static char *_php_math_number_format_ex_len(double d, int dec, char *dec_point,
1101+
size_t dec_point_len, char *thousand_sep, size_t thousand_sep_len,
1102+
int *result_len)
11011103
{
11021104
char *tmpbuf = NULL, *resbuf;
11031105
char *s, *t; /* source, target */
@@ -1205,8 +1207,19 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size
12051207

12061208
efree(tmpbuf);
12071209

1210+
if (result_len) {
1211+
*result_len = reslen;
1212+
}
1213+
12081214
return resbuf;
12091215
}
1216+
1217+
PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point,
1218+
size_t dec_point_len, char *thousand_sep, size_t thousand_sep_len)
1219+
{
1220+
return _php_math_number_format_ex_len(d, dec, dec_point, dec_point_len,
1221+
thousand_sep, thousand_sep_len, NULL);
1222+
}
12101223
/* }}} */
12111224

12121225
/* {{{ proto string number_format(float number [, int num_decimal_places [, string dec_seperator, string thousands_seperator]])
@@ -1241,7 +1254,10 @@ PHP_FUNCTION(number_format)
12411254
thousand_sep_len = 1;
12421255
}
12431256

1244-
RETURN_STRING(_php_math_number_format_ex(num, dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len), 0);
1257+
Z_TYPE_P(return_value) = IS_STRING;
1258+
Z_STRVAL_P(return_value) = _php_math_number_format_ex_len(num, dec,
1259+
dec_point, dec_point_len, thousand_sep, thousand_sep_len,
1260+
&Z_STRLEN_P(return_value));
12451261
break;
12461262
default:
12471263
WRONG_PARAM_COUNT;

ext/standard/tests/math/bug62112.phpt

143 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)