diff --git a/NEWS b/NEWS index aabe78ad2a7c..a98fc0c42c58 100644 --- a/NEWS +++ b/NEWS @@ -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) diff --git a/ext/standard/tests/serialize/gh25696_serialize_fiber_state.phpt b/ext/standard/tests/serialize/gh25696_serialize_fiber_state.phpt new file mode 100644 index 000000000000..a9c7f6e9da4f --- /dev/null +++ b/ext/standard/tests/serialize/gh25696_serialize_fiber_state.phpt @@ -0,0 +1,37 @@ +--TEST-- +GH-25696: serialize state survives a Fiber suspended during nested serialization +--FILE-- + 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;" diff --git a/ext/standard/tests/serialize/gh25696_unserialize_fiber_state.phpt b/ext/standard/tests/serialize/gh25696_unserialize_fiber_state.phpt new file mode 100644 index 000000000000..596476e8f2a2 --- /dev/null +++ b/ext/standard/tests/serialize/gh25696_unserialize_fiber_state.phpt @@ -0,0 +1,32 @@ +--TEST-- +GH-25696: unserialize state survives a Fiber suspended during nested unserialization +--FILE-- +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) diff --git a/ext/standard/var.c b/ext/standard/var.c index 48108f12e04c..2d188a8692aa 100644 --- a/ext/standard/var.c +++ b/ext/standard/var.c @@ -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) { @@ -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); @@ -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; @@ -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; @@ -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); diff --git a/ext/standard/var_unserializer.re b/ext/standard/var_unserializer.re index 5d0859ce1ed6..378a39ac1ee7 100644 --- a/ext/standard/var_unserializer.re +++ b/ext/standard/var_unserializer.re @@ -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) {