Skip to content

Commit fd911a7

Browse files
committed
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.
1 parent ef41eaa commit fd911a7

File tree

6 files changed

+44
-54
lines changed

6 files changed

+44
-54
lines changed

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ static const func_info_t func_infos[] = {
345345
F0("hypot", MAY_BE_DOUBLE),
346346
F0("deg2rad", MAY_BE_DOUBLE),
347347
F0("rad2deg", MAY_BE_DOUBLE),
348-
F0("bindec", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_DOUBLE),
349-
F0("hexdec", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_DOUBLE),
350-
F0("octdec", MAY_BE_FALSE | MAY_BE_LONG | MAY_BE_DOUBLE),
348+
F0("bindec", MAY_BE_LONG | MAY_BE_DOUBLE),
349+
F0("hexdec", MAY_BE_LONG | MAY_BE_DOUBLE),
350+
F0("octdec", MAY_BE_LONG | MAY_BE_DOUBLE),
351351
F1("decbin", MAY_BE_STRING),
352352
F1("decoct", MAY_BE_STRING),
353353
F1("dechex", MAY_BE_STRING),

ext/standard/math.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ PHPAPI zend_long _php_math_basetolong(zval *arg, int base)
843843
/*
844844
* Convert a string representation of a base(2-36) number to a zval.
845845
*/
846-
PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
846+
PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret)
847847
{
848848
zend_long num = 0;
849849
double fnum = 0;
@@ -853,16 +853,12 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
853853
zend_long cutoff;
854854
int cutlim;
855855

856-
if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
857-
return FAILURE;
858-
}
859-
860-
s = Z_STRVAL_P(arg);
856+
s = ZSTR_VAL(str);
861857

862858
cutoff = ZEND_LONG_MAX / base;
863859
cutlim = ZEND_LONG_MAX % base;
864860

865-
for (i = Z_STRLEN_P(arg); i > 0; i--) {
861+
for (i = ZSTR_LEN(str); i > 0; i--) {
866862
c = *s++;
867863

868864
/* might not work for EBCDIC */
@@ -898,7 +894,6 @@ PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
898894
} else {
899895
ZVAL_LONG(ret, num);
900896
}
901-
return SUCCESS;
902897
}
903898
/* }}} */
904899

@@ -972,54 +967,45 @@ PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base)
972967
}
973968
/* }}} */
974969

975-
/* {{{ proto int bindec(string binary_number)
970+
/* {{{ proto int|float bindec(string binary_number)
976971
Returns the decimal equivalent of the binary number */
977972
PHP_FUNCTION(bindec)
978973
{
979-
zval *arg;
974+
zend_string *arg;
980975

981976
ZEND_PARSE_PARAMETERS_START(1, 1)
982-
Z_PARAM_ZVAL(arg)
977+
Z_PARAM_STR(arg)
983978
ZEND_PARSE_PARAMETERS_END();
984979

985-
convert_to_string_ex(arg);
986-
if (_php_math_basetozval(arg, 2, return_value) == FAILURE) {
987-
RETURN_FALSE;
988-
}
980+
_php_math_basetozval(arg, 2, return_value);
989981
}
990982
/* }}} */
991983

992-
/* {{{ proto int hexdec(string hexadecimal_number)
984+
/* {{{ proto int|flat hexdec(string hexadecimal_number)
993985
Returns the decimal equivalent of the hexadecimal number */
994986
PHP_FUNCTION(hexdec)
995987
{
996-
zval *arg;
988+
zend_string *arg;
997989

998990
ZEND_PARSE_PARAMETERS_START(1, 1)
999-
Z_PARAM_ZVAL(arg)
991+
Z_PARAM_STR(arg)
1000992
ZEND_PARSE_PARAMETERS_END();
1001993

1002-
convert_to_string_ex(arg);
1003-
if (_php_math_basetozval(arg, 16, return_value) == FAILURE) {
1004-
RETURN_FALSE;
1005-
}
994+
_php_math_basetozval(arg, 16, return_value);
1006995
}
1007996
/* }}} */
1008997

1009-
/* {{{ proto int octdec(string octal_number)
998+
/* {{{ proto int|float octdec(string octal_number)
1010999
Returns the decimal equivalent of an octal string */
10111000
PHP_FUNCTION(octdec)
10121001
{
1013-
zval *arg;
1002+
zend_string *arg;
10141003

10151004
ZEND_PARSE_PARAMETERS_START(1, 1)
1016-
Z_PARAM_ZVAL(arg)
1005+
Z_PARAM_STR(arg)
10171006
ZEND_PARSE_PARAMETERS_END();
10181007

1019-
convert_to_string_ex(arg);
1020-
if (_php_math_basetozval(arg, 8, return_value) == FAILURE) {
1021-
RETURN_FALSE;
1022-
}
1008+
_php_math_basetozval(arg, 8, return_value);
10231009
}
10241010
/* }}} */
10251011

@@ -1098,9 +1084,7 @@ PHP_FUNCTION(base_convert)
10981084
RETURN_FALSE;
10991085
}
11001086

1101-
if(_php_math_basetozval(number, (int)frombase, &temp) == FAILURE) {
1102-
RETURN_FALSE;
1103-
}
1087+
_php_math_basetozval(Z_STR_P(number), (int)frombase, &temp);
11041088
result = _php_math_zvaltobase(&temp, (int)tobase);
11051089
RETVAL_STR(result);
11061090
}

ext/standard/php_math.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ PHPAPI zend_string *_php_math_number_format(double, int, char, char);
2525
PHPAPI zend_string *_php_math_number_format_ex(double, int, char *, size_t, char *, size_t);
2626
PHPAPI zend_string * _php_math_longtobase(zval *arg, int base);
2727
PHPAPI zend_long _php_math_basetolong(zval *arg, int base);
28-
PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret);
28+
PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret);
2929
PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base);
3030

3131
PHP_FUNCTION(sin);

ext/standard/tests/math/bindec_variation1_64bit.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ $inputs = array(
7373
$iterator = 1;
7474
foreach($inputs as $input) {
7575
echo "\n-- Iteration $iterator --\n";
76-
var_dump(bindec($input));
76+
try {
77+
var_dump(bindec($input));
78+
} catch (TypeError $e) {
79+
echo $e->getMessage(), "\n";
80+
}
7781
$iterator++;
7882
};
7983
fclose($fp);
8084
?>
8185
===Done===
82-
--EXPECTF--
86+
--EXPECT--
8387
*** Testing bindec() : usage variations ***
8488

8589
-- Iteration 1 --
@@ -134,9 +138,7 @@ int(0)
134138
int(0)
135139

136140
-- Iteration 18 --
137-
138-
Notice: Array to string conversion in %s on line %d
139-
int(0)
141+
bindec() expects parameter 1 to be string, array given
140142

141143
-- Iteration 19 --
142144
int(0)
@@ -154,5 +156,5 @@ int(0)
154156
int(0)
155157

156158
-- Iteration 24 --
157-
int(%d)
159+
bindec() expects parameter 1 to be string, resource given
158160
===Done===

ext/standard/tests/math/hexdec_variation1_64bit.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ $inputs = array(
7777
$iterator = 1;
7878
foreach($inputs as $input) {
7979
echo "\n-- Iteration $iterator --\n";
80-
var_dump(hexdec($input));
80+
try {
81+
var_dump(hexdec($input));
82+
} catch (TypeError $e) {
83+
echo $e->getMessage(), "\n";
84+
}
8185
$iterator++;
8286
};
8387
fclose($fp);
8488
?>
8589
===Done===
86-
--EXPECTF--
90+
--EXPECT--
8791
*** Testing hexdec() : usage variations ***
8892

8993
-- Iteration 1 --
@@ -144,9 +148,7 @@ int(0)
144148
int(0)
145149

146150
-- Iteration 20 --
147-
148-
Notice: Array to string conversion in %s on line %d
149-
int(170)
151+
hexdec() expects parameter 1 to be string, array given
150152

151153
-- Iteration 21 --
152154
int(2748)
@@ -164,5 +166,5 @@ int(0)
164166
int(0)
165167

166168
-- Iteration 26 --
167-
%s
169+
hexdec() expects parameter 1 to be string, resource given
168170
===Done===

ext/standard/tests/math/octdec_variation1.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ $inputs = array(
7373
$iterator = 1;
7474
foreach($inputs as $input) {
7575
echo "\n-- Iteration $iterator --\n";
76-
var_dump(octdec($input));
76+
try {
77+
var_dump(octdec($input));
78+
} catch (TypeError $e) {
79+
echo $e->getMessage(), "\n";
80+
}
7781
$iterator++;
7882
};
7983
fclose($fp);
8084
?>
8185
---Done---
82-
--EXPECTF--
86+
--EXPECT--
8387
*** Testing octdec() : usage variations ***
8488

8589
-- Iteration 1 --
@@ -140,9 +144,7 @@ int(0)
140144
int(0)
141145

142146
-- Iteration 20 --
143-
144-
Notice: Array to string conversion in %s on line %d
145-
int(0)
147+
octdec() expects parameter 1 to be string, array given
146148

147149
-- Iteration 21 --
148150
int(0)
@@ -160,5 +162,5 @@ int(0)
160162
int(0)
161163

162164
-- Iteration 26 --
163-
int(%d)
165+
octdec() expects parameter 1 to be string, resource given
164166
---Done---

0 commit comments

Comments
 (0)