Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ PHP NEWS
?? ??? ????, PHP 8.7.0alpha1

- Core:
. Fixed GH-25696 (serialize() and unserialize() state being freed while a
nested call in another Fiber is suspended). (Alessio Attilio)
. A flat unkeyed list assignment from an array literal with one value per
target, such as [$a, $b] = [$b, $a], no longer builds the array when the
result is unused (GH-23048). (Artem Ukrainskiy)
Expand Down
37 changes: 37 additions & 0 deletions ext/standard/tests/serialize/gh25696_serialize_fiber_state.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
GH-25696: serialize state survives a Fiber suspended during nested serialization
--FILE--
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

class Inner {
public function __serialize(): array {
Fiber::suspend();
return ['x' => 1];
}
}

class Outer implements Serializable {
public $fiber;

public function serialize(): string {
$this->fiber->start();
return 'x';
}

public function unserialize($data): void {}
}

$inner = new Inner;
$fiber = new Fiber(function () use ($inner) {
serialize($inner);
});
$outer = new Outer;
$outer->fiber = $fiber;

serialize($outer);
$fiber->resume();
var_dump(serialize(1));
?>
--EXPECT--
string(4) "i:1;"
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-25696: unserialize state survives a Fiber suspended during nested unserialization
--FILE--
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

function autoload($class) {
Fiber::suspend();
}

ini_set('unserialize_callback_func', 'autoload');

class Outer implements Serializable {
public function serialize(): string {
return '';
}

public function unserialize($data): void {
$GLOBALS['fiber']->start();
}
}

$GLOBALS['fiber'] = new Fiber(function () {
@unserialize('O:7:"Missing":0:{}');
});

$outer = unserialize(serialize(new Outer));
$GLOBALS['fiber']->resume();
var_dump(unserialize('i:1;'));
?>
--EXPECT--
int(1)
9 changes: 5 additions & 4 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ PHPAPI void php_var_serialize_set_incomplete_classes_only(php_serialize_data_t d

PHPAPI void php_var_serialize_destroy(php_serialize_data_t d) {
/* fprintf(stderr, "SERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(serialize).level); */
if (BG(serialize_lock)) {
if (d != BG(serialize).data) {
zend_hash_destroy(&d->ht);
efree(d);
} else if (!--BG(serialize).level) {
Expand Down Expand Up @@ -1495,6 +1495,7 @@ PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, co

p = (const unsigned char*) buf;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
bool nested = BG(unserialize).level > 1;

prev_class_hash = php_var_unserialize_get_allowed_classes(var_hash);
prev_max_depth = php_var_unserialize_get_max_depth(var_hash);
Expand Down Expand Up @@ -1558,7 +1559,7 @@ PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, co
}
}

if (BG(unserialize).level > 1) {
if (nested) {
retval = var_tmp_var(&var_hash);
} else {
retval = return_value;
Expand All @@ -1568,7 +1569,7 @@ PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, co
php_error_docref(NULL, E_WARNING, "Error at offset " ZEND_LONG_FMT " of %zd bytes",
(zend_long)((char*)p - buf), buf_len);
}
if (BG(unserialize).level <= 1) {
if (!nested) {
zval_ptr_dtor(return_value);
}
RETVAL_FALSE;
Expand All @@ -1579,7 +1580,7 @@ PHPAPI void php_unserialize_with_options(zval *return_value, const char *buf, co
(zend_long)((char*)p - buf), buf_len);
}
}
if (BG(unserialize).level > 1) {
if (nested) {
ZVAL_COPY(return_value, retval);
} else if (Z_REFCOUNTED_P(return_value)) {
zend_refcounted *ref = Z_COUNTED_P(return_value);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/var_unserializer.re
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ PHPAPI php_unserialize_data_t php_var_unserialize_init(void) {

PHPAPI void php_var_unserialize_destroy(php_unserialize_data_t d) {
/* fprintf(stderr, "UNSERIALIZE_DESTROY == lock: %u, level: %u\n", BG(serialize_lock), BG(unserialize).level); */
if (BG(serialize_lock)) {
if (d != BG(unserialize).data) {
var_destroy(&d);
efree(d);
} else if (!--BG(unserialize).level) {
Expand Down
Loading