Skip to content

Fix use-after-free in property coercion with __toString() #14971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 47 additions & 0 deletions Zend/tests/gh14969.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
GH-14969: Crash on coercion with throwing __toString()
--FILE--
<?php

class C {
public function __toString() {
global $c;
$c = [];
throw new Exception(__METHOD__);
}
}

class D {
public string $prop;
}

$c = new C();
$d = new D();
try {
$d->prop = $c;
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}
var_dump($d);

$c = new C();
$d->prop = 'foo';
try {
$d->prop = $c;
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}
var_dump($d);

?>
--EXPECTF--
C::__toString
object(D)#%d (0) {
["prop"]=>
uninitialized(string)
}
C::__toString
object(D)#2 (1) {
["prop"]=>
string(3) "foo"
}
4 changes: 2 additions & 2 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva

ZVAL_COPY_VALUE(&tmp, value);
if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, property_uses_strict_types()))) {
Z_TRY_DELREF_P(value);
zval_ptr_dtor(&tmp);
variable_ptr = &EG(error_zval);
goto exit;
}
Expand Down Expand Up @@ -890,7 +890,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva

ZVAL_COPY_VALUE(&tmp, value);
if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, property_uses_strict_types()))) {
zval_ptr_dtor(value);
zval_ptr_dtor(&tmp);
goto exit;
}
value = &tmp;
Expand Down
Loading