]> granicus.if.org Git - php/commitdiff
Expect string argument in hexdec, octdec, bindec
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 3 Jun 2019 08:27:15 +0000 (10:27 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 3 Jun 2019 09:26:25 +0000 (11:26 +0200)
Instead of accepting zval and converting to string. Also rewrite the
functions to make it obvious that they cannot return false.

ext/opcache/Optimizer/zend_func_info.c
ext/standard/math.c
ext/standard/php_math.h
ext/standard/tests/math/bindec_variation1_64bit.phpt
ext/standard/tests/math/hexdec_variation1_64bit.phpt
ext/standard/tests/math/octdec_variation1.phpt

index 588efd038374390542412be5b481980d7f477b39..efde5927f034aa6a111249b792fc88919a5c241b 100644 (file)
@@ -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),
index 94d657ad7dbb6c9902c1913c55e1d66319cd820e..769289945960964404b08a0a3be0cff8ab1490a4 100644 (file)
@@ -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);
 }
index 791a01c02df9c04e5fb249b27dca68f8f9c4b4b5..fd25cfb7f204f5e71fa5862364171d352df7e3fa 100644 (file)
@@ -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);
index 7fe9db234a38192538404ffdcdd8a71d954354a4..9387a5adfa0be0f4fffaf2d66029bb546111c28a 100644 (file)
@@ -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===
index 0ce9f6e39ab6f041123c7aa7e0399db96d2e2937..0c463ea7507e70f9c233cc46a1b69bf3c05ad000 100644 (file)
@@ -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===
index 42d6a7306848796b549102d548537213e1e616ea..cc8369c2aa7614c9b05eb3c229414164b2bdd3bf 100644 (file)
@@ -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---