Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ PHP 8.6 INTERNALS UPGRADE NOTES
. Added zend_string_ends_with() and related variants.
. Added trait support for internal classes.
. Added do_php_cli().
. Added zval_try_get_double(), which converts a zval to a double and reports
conversion failures through a bool pointer, analogous to
zval_try_get_long().

========================
2. Build system changes
Expand Down
69 changes: 69 additions & 0 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,75 @@ ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op) /* {{{ */
}
/* }}} */

static zend_never_inline double ZEND_FASTCALL zval_try_get_double_func(const zval *op, bool *failed) /* {{{ */
{
*failed = false;
try_again:
switch (Z_TYPE_P(op)) {
case IS_NULL:
case IS_FALSE:
return 0.0;
case IS_TRUE:
return 1.0;
case IS_LONG:
return (double) Z_LVAL_P(op);
case IS_DOUBLE:
return Z_DVAL_P(op);
case IS_STRING:
{
uint8_t type;
zend_long lval;
double dval;
bool trailing_data = false;

/* For BC reasons we allow errors so that we can warn on leading numeric string */
type = is_numeric_string_ex(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval,
/* allow errors */ true, NULL, &trailing_data);
if (type == 0) {
*failed = true;
return 0.0;
}
if (UNEXPECTED(trailing_data)) {
zend_error(E_WARNING, "A non-numeric value encountered");
if (UNEXPECTED(EG(exception))) {
*failed = true;
return 0.0;
}
}
return type == IS_LONG ? (double) lval : dval;
}
case IS_OBJECT:
{
zval dst;
if (Z_OBJ_HT_P(op)->cast_object(Z_OBJ_P(op), &dst, IS_DOUBLE) == FAILURE
|| EG(exception)) {
*failed = true;
return 0.0;
}
ZEND_ASSERT(Z_TYPE(dst) == IS_DOUBLE);
return Z_DVAL(dst);
}
case IS_RESOURCE:
case IS_ARRAY:
*failed = true;
return 0.0;
case IS_REFERENCE:
op = Z_REFVAL_P(op);
goto try_again;
default: ZEND_UNREACHABLE();
}
}
/* }}} */

ZEND_API double ZEND_FASTCALL zval_try_get_double(const zval *op, bool *failed)
{
if (EXPECTED(Z_TYPE_P(op) == IS_DOUBLE)) {
*failed = false;
return Z_DVAL_P(op);
}
return zval_try_get_double_func(op, failed);
}

static zend_always_inline zend_string* __zval_get_string_func(const zval *op, bool try) /* {{{ */
{
try_again:
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ ZEND_API void ZEND_FASTCALL convert_to_object(zval *op);
ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(const zval *op, bool is_strict);
ZEND_API zend_long ZEND_FASTCALL zval_try_get_long(const zval *op, bool *failed);
ZEND_API double ZEND_FASTCALL zval_get_double_func(const zval *op);
ZEND_API double ZEND_FASTCALL zval_try_get_double(const zval *op, bool *failed);
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(const zval *op);
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(const zval *op);

Expand Down
19 changes: 19 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,25 @@ static ZEND_FUNCTION(zend_test_refcount)
RETURN_LONG(Z_REFCOUNT_P(value));
}

static ZEND_FUNCTION(zend_test_zval_try_get_double)
{
zval *value;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(value)
ZEND_PARSE_PARAMETERS_END();

bool failed;
double result = zval_try_get_double(value, &failed);
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}

array_init(return_value);
add_assoc_double(return_value, "value", result);
add_assoc_bool(return_value, "failed", failed);
}

static ZEND_FUNCTION(zend_get_unit_enum)
{
ZEND_PARSE_PARAMETERS_NONE();
Expand Down
2 changes: 2 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ function zend_test_call_with_consumed_args(callable $cb, array $args, int $consu

function zend_test_refcount(mixed $value): int {}

function zend_test_zval_try_get_double(mixed $value): array {}

function zend_test_zend_ini_parse_quantity(string $str): int {}
function zend_test_zend_ini_parse_uquantity(string $str): int {}

Expand Down
8 changes: 7 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/zend_test/test_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion ext/zend_test/test_legacy_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions ext/zend_test/tests/zval_try_get_double.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
--TEST--
zval_try_get_double() conversion semantics
--EXTENSIONS--
zend_test
--FILE--
<?php

foreach ([
null,
false,
true,
42,
42.5,
"42",
"42.5",
"1e3",
"not numeric",
[],
new FloatCastableNoOperations(42.5),
new LongCastableNoOperations(42),
] as $value) {
var_dump(zend_test_zval_try_get_double($value));
}

$resource = fopen(__FILE__, 'r');
var_dump(zend_test_zval_try_get_double($resource));

var_dump(zend_test_zval_try_get_double("42 with trailing data"));

set_error_handler(static function (int $errno, string $errstr): never {
throw new Exception($errstr);
});
try {
zend_test_zval_try_get_double("42 with trailing data");
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

?>
--EXPECTF--
array(2) {
["value"]=>
float(0)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(0)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(1)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(42)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(42.5)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(42)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(42.5)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(1000)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(0)
["failed"]=>
bool(true)
}
array(2) {
["value"]=>
float(0)
["failed"]=>
bool(true)
}
array(2) {
["value"]=>
float(42.5)
["failed"]=>
bool(false)
}
array(2) {
["value"]=>
float(0)
["failed"]=>
bool(true)
}
array(2) {
["value"]=>
float(0)
["failed"]=>
bool(true)
}

Warning: A non-numeric value encountered in %s on line %d
array(2) {
["value"]=>
float(42)
["failed"]=>
bool(false)
}
Exception: A non-numeric value encountered
Loading