Skip to content
Prev Previous commit
Next Next commit
Handle strlen() in VM
  • Loading branch information
nikic committed Feb 11, 2021
commit fa4c2c2af58e09892429e7e56bb1dba0da06406f
12 changes: 12 additions & 0 deletions Zend/tests/null_to_non_nullable_special_func.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test null arg behavior for special functions
--FILE--
<?php

$null = null;
var_dump(strlen($null));

?>
--EXPECTF--
Deprecated: strlen(): Passing null to argument of type string is deprecated in %s on line %d
int(0)
2 changes: 1 addition & 1 deletion Zend/tests/nullsafe_operator/013.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dump_error(fn() => array_key_exists('foo', $foo?->foo()));

?>
--EXPECTF--
Deprecated: {closure}(): Passing null to argument of type string is deprecated in %s on line %d
Deprecated: strlen(): Passing null to argument of type string is deprecated in %s on line %d
int(0)
bool(true)
bool(false)
Expand Down
10 changes: 10 additions & 0 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -8310,6 +8310,16 @@ ZEND_VM_COLD_CONST_HANDLER(121, ZEND_STRLEN, CONST|TMPVAR|CV, ANY)
zend_string *str;
zval tmp;

if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
zend_error(E_DEPRECATED,
"strlen(): Passing null to argument of type string is deprecated");
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
ZVAL_LONG(EX_VAR(opline->result.var), 0);
break;
}

ZVAL_COPY(&tmp, value);
if (zend_parse_arg_str_weak(&tmp, &str)) {
ZVAL_LONG(EX_VAR(opline->result.var), ZSTR_LEN(str));
Expand Down
30 changes: 30 additions & 0 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -5286,6 +5286,16 @@ static ZEND_VM_COLD ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_STRLEN_SPEC_CONST
zend_string *str;
zval tmp;

if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
zend_error(E_DEPRECATED,
"strlen(): Passing null to argument of type string is deprecated");
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
ZVAL_LONG(EX_VAR(opline->result.var), 0);
break;
}

ZVAL_COPY(&tmp, value);
if (zend_parse_arg_str_weak(&tmp, &str)) {
ZVAL_LONG(EX_VAR(opline->result.var), ZSTR_LEN(str));
Expand Down Expand Up @@ -14459,6 +14469,16 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_STRLEN_SPEC_TMPVAR_HANDLER(ZEN
zend_string *str;
zval tmp;

if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
zend_error(E_DEPRECATED,
"strlen(): Passing null to argument of type string is deprecated");
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
ZVAL_LONG(EX_VAR(opline->result.var), 0);
break;
}

ZVAL_COPY(&tmp, value);
if (zend_parse_arg_str_weak(&tmp, &str)) {
ZVAL_LONG(EX_VAR(opline->result.var), ZSTR_LEN(str));
Expand Down Expand Up @@ -38546,6 +38566,16 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_STRLEN_SPEC_CV_HANDLER(ZEND_OP
zend_string *str;
zval tmp;

if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
zend_error(E_DEPRECATED,
"strlen(): Passing null to argument of type string is deprecated");
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
ZVAL_LONG(EX_VAR(opline->result.var), 0);
break;
}

ZVAL_COPY(&tmp, value);
if (zend_parse_arg_str_weak(&tmp, &str)) {
ZVAL_LONG(EX_VAR(opline->result.var), ZSTR_LEN(str));
Expand Down