Skip to content

Commit

Permalink
Fix leak if attribute arg evaluation fails while printing
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 14, 2021
1 parent 95da6e8 commit 882289e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6430,6 +6430,7 @@ ZEND_METHOD(ReflectionAttribute, __toString)
for (uint32_t i = 0; i < attr->data->argc; i++) {
zval tmp;
if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
smart_str_free(&str);
RETURN_THROWS();
}

Expand All @@ -6440,7 +6441,9 @@ ZEND_METHOD(ReflectionAttribute, __toString)
}

if (format_default_value(&str, &tmp, NULL) == FAILURE) {
return;
zval_ptr_dtor(&tmp);
smart_str_free(&str);
RETURN_THROWS();
}

smart_str_appends(&str, " ]\n");
Expand Down
12 changes: 10 additions & 2 deletions ext/reflection/tests/ReflectionAttribute_toString.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ ReflectionAttribute::__toString
--FILE--
<?php

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

$refl = new ReflectionFunction('foo');
echo $refl->getAttributes()[0];
echo $refl->getAttributes()[1];
echo $refl->getAttributes()[2];
--EXPECTF--
try {
echo $refl->getAttributes()[3];
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
Attribute [ Foo ]
Attribute [ Bar ] {
- Arguments [2] {
Expand All @@ -24,3 +31,4 @@ Attribute [ Baz ] {
Argument #1 [ 1234 ]
}
}
Undefined constant "ERROR"

0 comments on commit 882289e

Please sign in to comment.