Free unbound arguments when partial application construction throws#22788
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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) {} }'