Skip to content

Commit 882289e

Browse files
committed
Fix leak if attribute arg evaluation fails while printing
1 parent 95da6e8 commit 882289e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

ext/reflection/php_reflection.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6430,6 +6430,7 @@ ZEND_METHOD(ReflectionAttribute, __toString)
64306430
for (uint32_t i = 0; i < attr->data->argc; i++) {
64316431
zval tmp;
64326432
if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
6433+
smart_str_free(&str);
64336434
RETURN_THROWS();
64346435
}
64356436

@@ -6440,7 +6441,9 @@ ZEND_METHOD(ReflectionAttribute, __toString)
64406441
}
64416442

64426443
if (format_default_value(&str, &tmp, NULL) == FAILURE) {
6443-
return;
6444+
zval_ptr_dtor(&tmp);
6445+
smart_str_free(&str);
6446+
RETURN_THROWS();
64446447
}
64456448

64466449
smart_str_appends(&str, " ]\n");

ext/reflection/tests/ReflectionAttribute_toString.phpt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ ReflectionAttribute::__toString
33
--FILE--
44
<?php
55

6-
#[Foo, Bar(a: "foo", b: 1234), Baz("foo", 1234)]
6+
#[Foo, Bar(a: "foo", b: 1234), Baz("foo", 1234), XXX(ERROR)]
77
function foo() {}
88

99
$refl = new ReflectionFunction('foo');
1010
echo $refl->getAttributes()[0];
1111
echo $refl->getAttributes()[1];
1212
echo $refl->getAttributes()[2];
13-
--EXPECTF--
13+
try {
14+
echo $refl->getAttributes()[3];
15+
} catch (Error $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
?>
20+
--EXPECT--
1421
Attribute [ Foo ]
1522
Attribute [ Bar ] {
1623
- Arguments [2] {
@@ -24,3 +31,4 @@ Attribute [ Baz ] {
2431
Argument #1 [ 1234 ]
2532
}
2633
}
34+
Undefined constant "ERROR"

0 commit comments

Comments
 (0)