Skip to content

Commit 4877641

Browse files
committed
Fixed bug #73154
The object that is being serialized may be destroyed during the execution of __sleep(), so operate on a copy instead.
1 parent 9f560ba commit 4877641

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ PHP NEWS
4747
parameter). (Bruce Weirdan)
4848
. Fixed bug #70213 (Unserialize context shared on double class lookup).
4949
(Taoguang Chen)
50-
50+
. Fixed bug #73154 (serialize object with __sleep function crash). (Nikita)
5151

5252
- Zlib:
5353
. Fixed bug #73373 (deflate_add does not verify that output was not truncated).
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #73154: serialize object with __sleep function crash
3+
--FILE--
4+
<?php
5+
class a {
6+
public $a;
7+
public function __sleep() {
8+
$this->a=null;
9+
return array();
10+
}
11+
}
12+
$s = 'a:1:{i:0;O:1:"a":1:{s:1:"a";R:2;}}';
13+
var_dump(serialize(unserialize($s)));
14+
?>
15+
--EXPECT--
16+
string(22) "a:1:{i:0;O:1:"a":0:{}}"

ext/standard/var.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -860,9 +860,6 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
860860
return;
861861

862862
case IS_OBJECT: {
863-
zval retval;
864-
zval fname;
865-
int res;
866863
zend_class_entry *ce = Z_OBJCE_P(struc);
867864

868865
if (ce->serialize != NULL) {
@@ -891,32 +888,39 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
891888
}
892889

893890
if (ce != PHP_IC_ENTRY && zend_hash_str_exists(&ce->function_table, "__sleep", sizeof("__sleep")-1)) {
891+
zval fname, tmp, retval;
892+
int res;
893+
894+
ZVAL_COPY(&tmp, struc);
894895
ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") - 1);
895896
BG(serialize_lock)++;
896-
res = call_user_function_ex(CG(function_table), struc, &fname, &retval, 0, 0, 1, NULL);
897+
res = call_user_function_ex(CG(function_table), &tmp, &fname, &retval, 0, 0, 1, NULL);
897898
BG(serialize_lock)--;
898899
zval_dtor(&fname);
899900

900901
if (EG(exception)) {
901902
zval_ptr_dtor(&retval);
903+
zval_ptr_dtor(&tmp);
902904
return;
903905
}
904906

905907
if (res == SUCCESS) {
906908
if (Z_TYPE(retval) != IS_UNDEF) {
907909
if (HASH_OF(&retval)) {
908-
php_var_serialize_class(buf, struc, &retval, var_hash);
910+
php_var_serialize_class(buf, &tmp, &retval, var_hash);
909911
} else {
910912
php_error_docref(NULL, E_NOTICE, "__sleep should return an array only containing the names of instance-variables to serialize");
911913
/* we should still add element even if it's not OK,
912914
* since we already wrote the length of the array before */
913915
smart_str_appendl(buf,"N;", 2);
914916
}
915-
zval_ptr_dtor(&retval);
916917
}
918+
zval_ptr_dtor(&retval);
919+
zval_ptr_dtor(&tmp);
917920
return;
918921
}
919922
zval_ptr_dtor(&retval);
923+
zval_ptr_dtor(&tmp);
920924
}
921925

922926
/* fall-through */

0 commit comments

Comments
 (0)