Skip to content

Commit

Permalink
Fixed #74862: Unable to clone instance when private __clone defined
Browse files Browse the repository at this point in the history
Even though __clone was implemented as private and called only from
parent class, child extending class instance could not be cloned.
  • Loading branch information
DanielCiochiu authored and nikic committed Nov 15, 2017
1 parent c94d091 commit dba5a79
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PHP NEWS
- Core:
. Fixed bug #75384 (PHP seems incompatible with OneDrive files on demand).
(Anatol)
. Fixed bug #74862 (Unable to clone instance when private __clone defined).
(Daniel Ciochiu)

- CLI Server:
. Fixed bug #60471 (Random "Invalid request (unexpected EOF)" using a router
Expand Down
43 changes: 43 additions & 0 deletions Zend/tests/bug74862.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Bug #74862 (Unable to clone instance when private __clone defined)
--FILE--
<?php

class a {
private function __clone()
{

}

private function __construct()
{

}

public static function getInstance()
{
return new static();
}

public function cloneIt()
{
$a = clone $this;

return $a;
}
}

class c extends a {

}

// private constructor
$d = c::getInstance();

// private clone
$e = $d->cloneIt();
var_dump($e);
?>
--EXPECT--
object(c)#2 (0) {
}
46 changes: 46 additions & 0 deletions Zend/tests/bug74862_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Bug #74862 (Unable to clone instance when private __clone defined in a child class)
--FILE--
<?php

class main {
}

class a extends main {
private function __clone()
{

}

private function __construct()
{

}

public static function getInstance()
{
return new static();
}

public function cloneIt()
{
$a = clone $this;

return $a;
}
}

class c extends a {

}

// private constructor
$d = c::getInstance();

// private clone
$e = $d->cloneIt();
var_dump($e);
?>
--EXPECT--
object(c)#2 (0) {
}
6 changes: 3 additions & 3 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -5080,8 +5080,8 @@ ZEND_VM_HANDLER(110, ZEND_CLONE, CONST|TMPVAR|UNUSED|THIS|CV, ANY)
/* Ensure that if we're calling a private function, we're allowed to do so.
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(ce != scope)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
if (!zend_check_private(clone, scope, clone->common.function_name)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");
FREE_OP1();
HANDLE_EXCEPTION();
}
Expand All @@ -5090,7 +5090,7 @@ ZEND_VM_HANDLER(110, ZEND_CLONE, CONST|TMPVAR|UNUSED|THIS|CV, ANY)
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(clone), scope))) {
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");
FREE_OP1();
HANDLE_EXCEPTION();
}
Expand Down
24 changes: 12 additions & 12 deletions Zend/zend_vm_execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -3307,8 +3307,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_CONST_HANDLER(ZEND_
/* Ensure that if we're calling a private function, we're allowed to do so.
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(ce != scope)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
if (!zend_check_private(clone, scope, clone->common.function_name)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");

HANDLE_EXCEPTION();
}
Expand All @@ -3317,7 +3317,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_CONST_HANDLER(ZEND_
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(clone), scope))) {
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");

HANDLE_EXCEPTION();
}
Expand Down Expand Up @@ -28031,8 +28031,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_UNUSED_HANDLER(ZEND
/* Ensure that if we're calling a private function, we're allowed to do so.
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(ce != scope)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
if (!zend_check_private(clone, scope, clone->common.function_name)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");

HANDLE_EXCEPTION();
}
Expand All @@ -28041,7 +28041,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_UNUSED_HANDLER(ZEND
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(clone), scope))) {
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");

HANDLE_EXCEPTION();
}
Expand Down Expand Up @@ -35361,8 +35361,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_CV_HANDLER(ZEND_OPC
/* Ensure that if we're calling a private function, we're allowed to do so.
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(ce != scope)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
if (!zend_check_private(clone, scope, clone->common.function_name)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");

HANDLE_EXCEPTION();
}
Expand All @@ -35371,7 +35371,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_CV_HANDLER(ZEND_OPC
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(clone), scope))) {
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");

HANDLE_EXCEPTION();
}
Expand Down Expand Up @@ -51620,8 +51620,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_TMPVAR_HANDLER(ZEND
/* Ensure that if we're calling a private function, we're allowed to do so.
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(ce != scope)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
if (!zend_check_private(clone, scope, clone->common.function_name)) {
zend_throw_error(NULL, "Call to private %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");
zval_ptr_dtor_nogc(free_op1);
HANDLE_EXCEPTION();
}
Expand All @@ -51630,7 +51630,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_CLONE_SPEC_TMPVAR_HANDLER(ZEND
*/
scope = EX(func)->op_array.scope;
if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(clone), scope))) {
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(ce->name), scope ? ZSTR_VAL(scope->name) : "");
zend_throw_error(NULL, "Call to protected %s::__clone() from context '%s'", ZSTR_VAL(clone->common.scope->name), scope ? ZSTR_VAL(scope->name) : "");
zval_ptr_dtor_nogc(free_op1);
HANDLE_EXCEPTION();
}
Expand Down

0 comments on commit dba5a79

Please sign in to comment.