Skip to content

Commit

Permalink
Fix STR_OR_OBJ_OF_TYPE stringable handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 29, 2020
1 parent 213494f commit 993be4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
9 changes: 9 additions & 0 deletions Zend/tests/str_or_obj_of_class_zpp.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ if (!extension_loaded('zend-test')) die('skip zend-test extension not loaded');
<?php

class Foo {}
class ToString {
public function __toString() {
return "ToString";
}
}

var_dump(zend_string_or_stdclass("string"));
var_dump(zend_string_or_stdclass(1));
var_dump(zend_string_or_stdclass(null));
var_dump(zend_string_or_stdclass(new stdClass()));
var_dump(zend_string_or_stdclass(new ToString()));

try {
zend_string_or_stdclass([]);
Expand All @@ -30,6 +36,7 @@ var_dump(zend_string_or_stdclass_or_null("string"));
var_dump(zend_string_or_stdclass_or_null(1));
var_dump(zend_string_or_stdclass_or_null(null));
var_dump(zend_string_or_stdclass_or_null(new stdClass()));
var_dump(zend_string_or_stdclass_or_null(new ToString()));

try {
zend_string_or_stdclass_or_null([]);
Expand All @@ -50,12 +57,14 @@ string(1) "1"
string(0) ""
object(stdClass)#1 (0) {
}
string(8) "ToString"
zend_string_or_stdclass(): Argument #1 ($param) must be of type stdClass|string, array given
zend_string_or_stdclass(): Argument #1 ($param) must be of type stdClass|string, Foo given
string(6) "string"
string(1) "1"
NULL
object(stdClass)#1 (0) {
}
string(8) "ToString"
zend_string_or_stdclass_or_null(): Argument #1 ($param) must be of type stdClass|string|null, array given
zend_string_or_stdclass_or_null(): Argument #1 ($param) must be of type stdClass|string|null, Foo given
21 changes: 6 additions & 15 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -2014,24 +2014,15 @@ static zend_always_inline int zend_parse_arg_str_or_obj(
zval *arg, zend_string **destination_string, zend_object **destination_object, zend_class_entry *base_ce, int allow_null
) {
if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
if (base_ce && UNEXPECTED(!instanceof_function(Z_OBJCE_P(arg), base_ce))) {
return 0;
if (!base_ce || EXPECTED(instanceof_function(Z_OBJCE_P(arg), base_ce))) {
*destination_string = NULL;
*destination_object = Z_OBJ_P(arg);
return 1;
}

*destination_string = NULL;
*destination_object = Z_OBJ_P(arg);
} else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
*destination_string = Z_STR_P(arg);
*destination_object = NULL;
} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
*destination_string = NULL;
*destination_object = NULL;
} else {
*destination_object = NULL;
return zend_parse_arg_str_slow(arg, destination_string);
}

return 1;
*destination_object = NULL;
return zend_parse_arg_str(arg, destination_string, allow_null);
}

END_EXTERN_C()
Expand Down

0 comments on commit 993be4b

Please sign in to comment.