Skip to content

Free unbound arguments when partial application construction throws#181

Closed
iliaal wants to merge 5 commits into
masterfrom
fix/pfa-partial-arg-leak
Closed

Free unbound arguments when partial application construction throws#181
iliaal wants to merge 5 commits into
masterfrom
fix/pfa-partial-arg-leak

Conversation

@iliaal

@iliaal iliaal commented Jul 17, 2026

Copy link
Copy Markdown
Owner

The ZEND_CALLABLE_CONVERT_PARTIAL handler relies on zp_bind() to move each pre-bound argument into the partial closure and never frees positional arguments itself. When construction throws before an argument is moved, either a NULL op_array (argument count error, unknown default value, forbidden dynamic call) or a bound-argument type error, the unmoved arguments were owned by nobody and leaked. A try/catch loop over a failing partial with refcounted arguments leaks within the request until memory_limit. Free the arguments that were not moved at each throwing path.

Reproducer, definitely-lost blocks under valgrind before the fix, clean after:

php -r 'function f(int $a, $b, $c){} for ($i=0;$i<200;$i++){ try { f([1], new stdClass, ?); } catch (\Throwable $e) {} }'

Sjord and others added 5 commits July 17, 2026 14:30
Assert and take advantage of that size is always 1. This is also documented in [the curl docs](https://curl.se/libcurl/c/CURLOPT_READFUNCTION.html). So there is no need to multiply with size, so this can be removed.

I started this to see whether size * nmemb can overflow. This makes it clear that it cannot.
And instead let the normal docref error handling code handle arguments
The ZEND_CALLABLE_CONVERT_PARTIAL handler relies on zp_bind() to move
each pre-bound argument into the partial and never frees positional
arguments itself. When construction throws before an argument is moved,
a NULL op_array from zp_get_op_array() or a bound-argument type error,
the unmoved arguments were owned by nobody and leaked. Free the
arguments that were not moved at each throwing path.
@iliaal
iliaal force-pushed the fix/pfa-partial-arg-leak branch from 7fd0035 to 9f56efe Compare July 17, 2026 14:41
@iliaal

iliaal commented Jul 17, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream: php#22788.

@iliaal iliaal closed this Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants