Skip to content

Commit 3f8ef07

Browse files
author
Sjoerd Langkemper
committed
Throw error for invalid characters for number base
This is about the situation where a character is not within the specified number base. E.g. only 0-9, a-f are allowed for hexadecimal, so what should `hexdec('z')` do? In the past, such characters were silently ignored. Since PHP 7.4 a deprecation notice would be generated. This commit changes that to a ValueError. Earlier RFC: https://wiki.php.net/rfc/base_convert_improvements
1 parent e71b4e5 commit 3f8ef07

17 files changed

Lines changed: 323 additions & 596 deletions

ext/standard/math.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ PHPAPI zend_long _php_math_basetolong(zval *arg, int base)
845845
/*
846846
* Convert a string representation of a base(2-36) number to a zval.
847847
*/
848-
PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret)
848+
PHPAPI zend_result _php_math_basetozval(zend_string *str, int base, zval *ret)
849849
{
850850
zend_long num = 0;
851851
double fnum = 0;
@@ -908,14 +908,16 @@ PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret)
908908
}
909909

910910
if (invalidchars > 0) {
911-
zend_error(E_DEPRECATED, "Invalid characters passed for attempted conversion, these have been ignored");
911+
zend_value_error("Invalid characters passed for attempted conversion");
912+
return FAILURE;
912913
}
913914

914915
if (mode == 1) {
915916
ZVAL_DOUBLE(ret, fnum);
916917
} else {
917918
ZVAL_LONG(ret, num);
918919
}
920+
return SUCCESS;
919921
}
920922
/* }}} */
921923

@@ -1033,7 +1035,9 @@ PHP_FUNCTION(bindec)
10331035
Z_PARAM_STR(arg)
10341036
ZEND_PARSE_PARAMETERS_END();
10351037

1036-
_php_math_basetozval(arg, 2, return_value);
1038+
if (SUCCESS != _php_math_basetozval(arg, 2, return_value)) {
1039+
RETURN_THROWS();
1040+
}
10371041
}
10381042
/* }}} */
10391043

@@ -1046,7 +1050,9 @@ PHP_FUNCTION(hexdec)
10461050
Z_PARAM_STR(arg)
10471051
ZEND_PARSE_PARAMETERS_END();
10481052

1049-
_php_math_basetozval(arg, 16, return_value);
1053+
if (SUCCESS != _php_math_basetozval(arg, 16, return_value)) {
1054+
RETURN_THROWS();
1055+
}
10501056
}
10511057
/* }}} */
10521058

@@ -1059,7 +1065,9 @@ PHP_FUNCTION(octdec)
10591065
Z_PARAM_STR(arg)
10601066
ZEND_PARSE_PARAMETERS_END();
10611067

1062-
_php_math_basetozval(arg, 8, return_value);
1068+
if (SUCCESS != _php_math_basetozval(arg, 8, return_value)) {
1069+
RETURN_THROWS();
1070+
}
10631071
}
10641072
/* }}} */
10651073

@@ -1136,7 +1144,10 @@ PHP_FUNCTION(base_convert)
11361144
RETURN_THROWS();
11371145
}
11381146

1139-
_php_math_basetozval(number, (int)frombase, &temp);
1147+
if (SUCCESS != _php_math_basetozval(number, (int)frombase, &temp)) {
1148+
RETURN_THROWS();
1149+
}
1150+
11401151
result = _php_math_zvaltobase(&temp, (int)tobase);
11411152
if (!result) {
11421153
RETURN_THROWS();

ext/standard/php_math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, const char *de
2222
PHPAPI zend_string *_php_math_number_format_long(zend_long num, zend_long dec, const char *dec_point, size_t dec_point_len, const char *thousand_sep, size_t thousand_sep_len);
2323
PHPAPI zend_string * _php_math_longtobase(zend_long arg, int base);
2424
PHPAPI zend_long _php_math_basetolong(zval *arg, int base);
25-
PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret);
25+
PHPAPI zend_result _php_math_basetozval(zend_string *str, int base, zval *ret);
2626
PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base);
2727

2828
#include <math.h>

0 commit comments

Comments
 (0)