summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-06-03 10:27:15 +0200
committerNikita Popov <nikita.ppv@gmail.com>2019-06-03 11:26:25 +0200
commitfd911a712424dc9c33a6a8ec415f72a645a58790 (patch)
tree28225c69babf6321694f68fb9f3124dccbd3553a
parentef41eaa5c6b7305ad1760ecd43ae35aca6dce9c2 (diff)
downloadphp-git-fd911a712424dc9c33a6a8ec415f72a645a58790.tar.gz
Expect string argument in hexdec, octdec, bindec
Instead of accepting zval and converting to string. Also rewrite the functions to make it obvious that they cannot return false.
-rw-r--r--ext/opcache/Optimizer/zend_func_info.c6
-rw-r--r--ext/standard/math.c48
-rw-r--r--ext/standard/php_math.h2
-rw-r--r--ext/standard/tests/math/bindec_variation1_64bit.phpt14
-rw-r--r--ext/standard/tests/math/hexdec_variation1_64bit.phpt14
-rw-r--r--ext/standard/tests/math/octdec_variation1.phpt14
6 files changed, 44 insertions, 54 deletions
diff --git a/ext/opcache/Optimizer/zend_func_info.c b/ext/opcache/Optimizer/zend_func_info.c
index 588efd0383..efde5927f0 100644
--- a/ext/opcache/Optimizer/zend_func_info.c
+++ b/ext/opcache/Optimizer/zend_func_info.c
@@ -345,9 +345,9 @@ static const func_info_t func_infos[] = {
F0("hypot", MAY_BE_DOUBLE),
F0("deg2rad", MAY_BE_DOUBLE),
F0("rad2deg", MAY_BE_DOUBLE),
- F0("bindec", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_DOUBLE),
- F0("hexdec", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_DOUBLE),
- F0("octdec", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_DOUBLE),
+ F0("bindec", MAY_BE_LONG | MAY_BE_DOUBLE),
+ F0("hexdec", MAY_BE_LONG | MAY_BE_DOUBLE),
+ F0("octdec", MAY_BE_LONG | MAY_BE_DOUBLE),
F1("decbin", MAY_BE_STRING),
F1("decoct", MAY_BE_STRING),
F1("dechex", MAY_BE_STRING),
diff --git a/ext/standard/math.c b/ext/standard/math.c
index 94d657ad7d..7692899459 100644
--- a/ext/standard/math.c
+++ b/ext/standard/math.c
@@ -843,7 +843,7 @@ PHPAPI zend_long _php_math_basetolong(zval *arg, int base)
/*
* Convert a string representation of a base(2-36) number to a zval.
*/
-PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
+PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret)
{
zend_long num = 0;
double fnum = 0;
@@ -853,16 +853,12 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
zend_long cutoff;
int cutlim;
- if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
- return FAILURE;
- }
-
- s = Z_STRVAL_P(arg);
+ s = ZSTR_VAL(str);
cutoff = ZEND_LONG_MAX / base;
cutlim = ZEND_LONG_MAX % base;
- for (i = Z_STRLEN_P(arg); i > 0; i--) {
+ for (i = ZSTR_LEN(str); i > 0; i--) {
c = *s++;
/* might not work for EBCDIC */
@@ -898,7 +894,6 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
} else {
ZVAL_LONG(ret, num);
}
- return SUCCESS;
}
/* }}} */
@@ -972,54 +967,45 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base)
}
/* }}} */
-/* {{{ proto int bindec(string binary_number)
+/* {{{ proto int|float bindec(string binary_number)
Returns the decimal equivalent of the binary number */
PHP_FUNCTION(bindec)
{
- zval *arg;
+ zend_string *arg;
ZEND_PARSE_PARAMETERS_START(1, 1)
- Z_PARAM_ZVAL(arg)
+ Z_PARAM_STR(arg)
ZEND_PARSE_PARAMETERS_END();
- convert_to_string_ex(arg);
- if (_php_math_basetozval(arg, 2, return_value) == FAILURE) {
- RETURN_FALSE;
- }
+ _php_math_basetozval(arg, 2, return_value);
}
/* }}} */
-/* {{{ proto int hexdec(string hexadecimal_number)
+/* {{{ proto int|flat hexdec(string hexadecimal_number)
Returns the decimal equivalent of the hexadecimal number */
PHP_FUNCTION(hexdec)
{
- zval *arg;
+ zend_string *arg;
ZEND_PARSE_PARAMETERS_START(1, 1)
- Z_PARAM_ZVAL(arg)
+ Z_PARAM_STR(arg)
ZEND_PARSE_PARAMETERS_END();
- convert_to_string_ex(arg);
- if (_php_math_basetozval(arg, 16, return_value) == FAILURE) {
- RETURN_FALSE;
- }
+ _php_math_basetozval(arg, 16, return_value);
}
/* }}} */
-/* {{{ proto int octdec(string octal_number)
+/* {{{ proto int|float octdec(string octal_number)
Returns the decimal equivalent of an octal string */
PHP_FUNCTION(octdec)
{
- zval *arg;
+ zend_string *arg;
ZEND_PARSE_PARAMETERS_START(1, 1)
- Z_PARAM_ZVAL(arg)
+ Z_PARAM_STR(arg)
ZEND_PARSE_PARAMETERS_END();
- convert_to_string_ex(arg);
- if (_php_math_basetozval(arg, 8, return_value) == FAILURE) {
- RETURN_FALSE;
- }
+ _php_math_basetozval(arg, 8, return_value);
}
/* }}} */
@@ -1098,9 +1084,7 @@ PHP_FUNCTION(base_convert)
RETURN_FALSE;
}
- if(_php_math_basetozval(number, (int)frombase, &temp) == FAILURE) {
- RETURN_FALSE;
- }
+ _php_math_basetozval(Z_STR_P(number), (int)frombase, &temp);
result = _php_math_zvaltobase(&temp, (int)tobase);
RETVAL_STR(result);
}
diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h
index 791a01c02d..fd25cfb7f2 100644
--- a/ext/standard/php_math.h
+++ b/ext/standard/php_math.h
@@ -25,7 +25,7 @@ PHPAPI zend_string *_php_math_number_format(double, int, char, char);
PHPAPI zend_string *_php_math_number_format_ex(double, int, char *, size_t, char *, size_t);
PHPAPI zend_string * _php_math_longtobase(zval *arg, int base);
PHPAPI zend_long _php_math_basetolong(zval *arg, int base);
-PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret);
+PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret);
PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base);
PHP_FUNCTION(sin);
diff --git a/ext/standard/tests/math/bindec_variation1_64bit.phpt b/ext/standard/tests/math/bindec_variation1_64bit.phpt
index 7fe9db234a..9387a5adfa 100644
--- a/ext/standard/tests/math/bindec_variation1_64bit.phpt
+++ b/ext/standard/tests/math/bindec_variation1_64bit.phpt
@@ -73,13 +73,17 @@ $inputs = array(
$iterator = 1;
foreach($inputs as $input) {
echo "\n-- Iteration $iterator --\n";
- var_dump(bindec($input));
+ try {
+ var_dump(bindec($input));
+ } catch (TypeError $e) {
+ echo $e->getMessage(), "\n";
+ }
$iterator++;
};
fclose($fp);
?>
===Done===
---EXPECTF--
+--EXPECT--
*** Testing bindec() : usage variations ***
-- Iteration 1 --
@@ -134,9 +138,7 @@ int(0)
int(0)
-- Iteration 18 --
-
-Notice: Array to string conversion in %s on line %d
-int(0)
+bindec() expects parameter 1 to be string, array given
-- Iteration 19 --
int(0)
@@ -154,5 +156,5 @@ int(0)
int(0)
-- Iteration 24 --
-int(%d)
+bindec() expects parameter 1 to be string, resource given
===Done===
diff --git a/ext/standard/tests/math/hexdec_variation1_64bit.phpt b/ext/standard/tests/math/hexdec_variation1_64bit.phpt
index 0ce9f6e39a..0c463ea750 100644
--- a/ext/standard/tests/math/hexdec_variation1_64bit.phpt
+++ b/ext/standard/tests/math/hexdec_variation1_64bit.phpt
@@ -77,13 +77,17 @@ $inputs = array(
$iterator = 1;
foreach($inputs as $input) {
echo "\n-- Iteration $iterator --\n";
- var_dump(hexdec($input));
+ try {
+ var_dump(hexdec($input));
+ } catch (TypeError $e) {
+ echo $e->getMessage(), "\n";
+ }
$iterator++;
};
fclose($fp);
?>
===Done===
---EXPECTF--
+--EXPECT--
*** Testing hexdec() : usage variations ***
-- Iteration 1 --
@@ -144,9 +148,7 @@ int(0)
int(0)
-- Iteration 20 --
-
-Notice: Array to string conversion in %s on line %d
-int(170)
+hexdec() expects parameter 1 to be string, array given
-- Iteration 21 --
int(2748)
@@ -164,5 +166,5 @@ int(0)
int(0)
-- Iteration 26 --
-%s
+hexdec() expects parameter 1 to be string, resource given
===Done===
diff --git a/ext/standard/tests/math/octdec_variation1.phpt b/ext/standard/tests/math/octdec_variation1.phpt
index 42d6a73068..cc8369c2aa 100644
--- a/ext/standard/tests/math/octdec_variation1.phpt
+++ b/ext/standard/tests/math/octdec_variation1.phpt
@@ -73,13 +73,17 @@ $inputs = array(
$iterator = 1;
foreach($inputs as $input) {
echo "\n-- Iteration $iterator --\n";
- var_dump(octdec($input));
+ try {
+ var_dump(octdec($input));
+ } catch (TypeError $e) {
+ echo $e->getMessage(), "\n";
+ }
$iterator++;
};
fclose($fp);
?>
---Done---
---EXPECTF--
+--EXPECT--
*** Testing octdec() : usage variations ***
-- Iteration 1 --
@@ -140,9 +144,7 @@ int(0)
int(0)
-- Iteration 20 --
-
-Notice: Array to string conversion in %s on line %d
-int(0)
+octdec() expects parameter 1 to be string, array given
-- Iteration 21 --
int(0)
@@ -160,5 +162,5 @@ int(0)
int(0)
-- Iteration 26 --
-int(%d)
+octdec() expects parameter 1 to be string, resource given
---Done---