Skip to content

Fixed Bug #60977 number_format behavior changed when passing \0 for 4th parameter. #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -1094,10 +1094,10 @@ PHP_FUNCTION(base_convert)
*/
PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep)
{
return _php_math_number_format_ex(d, dec, &dec_point, 1, &thousand_sep, 1);
return _php_math_number_format_ex(d, dec, &dec_point, &thousand_sep);
}

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)
PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, char *thousand_sep)
{
char *tmpbuf = NULL, *resbuf;
char *s, *t; /* source, target */
Expand All @@ -1106,6 +1106,8 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size
int tmplen, reslen=0;
int count=0;
int is_negative=0;
size_t dec_point_len = strlen(dec_point);
size_t thousand_sep_len = strlen(thousand_sep);

if (d < 0) {
is_negative = 1;
Expand Down Expand Up @@ -1137,7 +1139,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size
}

/* allow for thousand separators */
if (thousand_sep) {
if (thousand_sep_len) {
integral += thousand_sep_len * ((integral-1) / 3);
}

Expand All @@ -1146,7 +1148,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size
if (dec) {
reslen += dec;

if (dec_point) {
if (dec_point_len) {
reslen += dec_point_len;
}
}
Expand Down Expand Up @@ -1182,7 +1184,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size
}

/* add decimal point */
if (dec_point) {
if (dec_point_len) {
t -= dec_point_len;
memcpy(t + 1, dec_point, dec_point_len);
}
Expand All @@ -1192,7 +1194,7 @@ PHPAPI char *_php_math_number_format_ex(double d, int dec, char *dec_point, size
* separator every three digits */
while(s >= tmpbuf) {
*t-- = *s--;
if (thousand_sep && (++count%3)==0 && s>=tmpbuf) {
if (thousand_sep_len && (++count%3)==0 && s>=tmpbuf) {
t -= thousand_sep_len;
memcpy(t + 1, thousand_sep, thousand_sep_len);
}
Expand Down Expand Up @@ -1233,15 +1235,13 @@ PHP_FUNCTION(number_format)
case 4:
if (dec_point == NULL) {
dec_point = &dec_point_chr;
dec_point_len = 1;
}

if (thousand_sep == NULL) {
thousand_sep = &thousand_sep_chr;
thousand_sep_len = 1;
}

RETURN_STRING(_php_math_number_format_ex(num, dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len), 0);
RETURN_STRING(_php_math_number_format_ex(num, dec, dec_point, thousand_sep), 0);
break;
default:
WRONG_PARAM_COUNT;
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/php_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define PHP_MATH_H

PHPAPI char *_php_math_number_format(double, int, char, char);
PHPAPI char *_php_math_number_format_ex(double, int, char *, size_t, char *, size_t);
PHPAPI char *_php_math_number_format_ex(double, int, char *, char *);
PHPAPI char * _php_math_longtobase(zval *arg, int base);
PHPAPI long _php_math_basetolong(zval *arg, int base);
PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret);
Expand Down
21 changes: 21 additions & 0 deletions ext/standard/tests/math/number_format_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, ',' , ' ');
var_dump($res);
}

echo "\n number_format tests.....Null thousand separater\n";
for ($i = 0; $i < count($values); $i++) {
$res = number_format($values[$i], 2, '.', "\0");
var_dump($res);
}

?>
--EXPECTF--
number_format tests.....default
Expand Down Expand Up @@ -95,3 +102,17 @@ string(6) "123,46"
string(4) "0,00"
string(4) "1,00"
string(4) "0,00"

number_format tests.....Null thousand separater
string(7) "1234.57"
string(8) "-1234.57"
string(11) "12345678.00"
string(12) "-12345678.90"
string(12) "305450479.00"
string(12) "402653183.00"
string(12) "123456789.00"
string(6) "123.46"
string(6) "123.46"
string(4) "0.00"
string(4) "1.00"
string(4) "0.00"