Skip to content

Commit be4ce98

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 1d6f934 commit be4ce98

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed
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
@@ -862,9 +862,6 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
862862
return;
863863

864864
case IS_OBJECT: {
865-
zval retval;
866-
zval fname;
867-
int res;
868865
zend_class_entry *ce = Z_OBJCE_P(struc);
869866

870867
if (ce->serialize != NULL) {
@@ -893,32 +890,39 @@ static void php_var_serialize_intern(smart_str *buf, zval *struc, php_serialize_
893890
}
894891

895892
if (ce != PHP_IC_ENTRY && zend_hash_str_exists(&ce->function_table, "__sleep", sizeof("__sleep")-1)) {
893+
zval fname, tmp, retval;
894+
int res;
895+
896+
ZVAL_COPY(&tmp, struc);
896897
ZVAL_STRINGL(&fname, "__sleep", sizeof("__sleep") - 1);
897898
BG(serialize_lock)++;
898-
res = call_user_function_ex(CG(function_table), struc, &fname, &retval, 0, 0, 1, NULL);
899+
res = call_user_function_ex(CG(function_table), &tmp, &fname, &retval, 0, 0, 1, NULL);
899900
BG(serialize_lock)--;
900901
zval_dtor(&fname);
901902

902903
if (EG(exception)) {
903904
zval_ptr_dtor(&retval);
905+
zval_ptr_dtor(&tmp);
904906
return;
905907
}
906908

907909
if (res == SUCCESS) {
908910
if (Z_TYPE(retval) != IS_UNDEF) {
909911
if (HASH_OF(&retval)) {
910-
php_var_serialize_class(buf, struc, &retval, var_hash);
912+
php_var_serialize_class(buf, &tmp, &retval, var_hash);
911913
} else {
912914
php_error_docref(NULL, E_NOTICE, "__sleep should return an array only containing the names of instance-variables to serialize");
913915
/* we should still add element even if it's not OK,
914916
* since we already wrote the length of the array before */
915917
smart_str_appendl(buf,"N;", 2);
916918
}
917-
zval_ptr_dtor(&retval);
918919
}
920+
zval_ptr_dtor(&retval);
921+
zval_ptr_dtor(&tmp);
919922
return;
920923
}
921924
zval_ptr_dtor(&retval);
925+
zval_ptr_dtor(&tmp);
922926
}
923927

924928
/* fall-through */

0 commit comments

Comments
 (0)