mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
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.
This commit is contained in:
parent
ef41eaa5c6
commit
fd911a7124
6 changed files with 44 additions and 54 deletions
|
@ -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),
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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===
|
||||
|
|
|
@ -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===
|
||||
|
|
|
@ -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---
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue