-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Fix memory leak when user filter does not fully consume the input brigade buckets #20058
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
Open
ndossche
wants to merge
5
commits into
php:PHP-8.4
Choose a base branch
from
ndossche:user-filter-leak-1
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−15
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85636de
Fix memory leak when user filter does not fully consume the input bri…
ndossche df88b7e
Move broken contract logic
ndossche cdc021d
Keep input buckets a PSFS_FEED_ME filter put back, and drain output f…
matthiasgoergens df4e0f3
Free buckets abandoned by callers on PSFS_FEED_ME
iliaal 4526f72
Fix extra leak
ndossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
ext/standard/tests/filters/user_filter_feed_me_keeps_input.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --TEST-- | ||
| User filter returning PSFS_FEED_ME keeps buckets it put back on the input brigade | ||
| --FILE-- | ||
| <?php | ||
| class defer extends php_user_filter { | ||
| private int $calls = 0; | ||
| public function filter($in, $out, &$consumed, bool $closing): int { | ||
| $this->calls++; | ||
| $buckets = []; | ||
| while ($b = stream_bucket_make_writeable($in)) { $buckets[] = $b; } | ||
| if ($this->calls < 3) { | ||
| foreach ($buckets as $b) { stream_bucket_prepend($in, $b); } | ||
| return PSFS_FEED_ME; | ||
| } | ||
| foreach ($buckets as $b) { stream_bucket_append($out, $b); } | ||
| return PSFS_PASS_ON; | ||
| } | ||
| } | ||
| stream_filter_register("defer", "defer"); | ||
| $f = __DIR__ . "/user_filter_feed_me_keeps_input.bin"; | ||
| file_put_contents($f, str_repeat("a", 8192) . str_repeat("b", 8192) . str_repeat("c", 8192)); | ||
| $fp = fopen($f, 'r'); | ||
| stream_filter_append($fp, "defer", STREAM_FILTER_READ); | ||
| var_dump(strlen(stream_get_contents($fp))); | ||
| fclose($fp); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| @unlink(__DIR__ . "/user_filter_feed_me_keeps_input.bin"); | ||
| ?> | ||
| --EXPECT-- | ||
| int(24576) |
22 changes: 22 additions & 0 deletions
22
ext/standard/tests/filters/user_filter_no_consume_leak.phpt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| --TEST-- | ||
| User filter that does not consume any input bucket leaks | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class noop_filter extends php_user_filter { | ||
| public function filter($in, $out, &$consumed, bool $closing): int { | ||
| return PSFS_PASS_ON; | ||
| } | ||
| } | ||
|
|
||
| stream_filter_register("noop_filter", "noop_filter"); | ||
|
|
||
| stream_filter_append(STDOUT, "noop_filter"); | ||
|
|
||
| $out = fwrite(STDOUT, "Hello\n"); | ||
| var_dump($out); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d | ||
| int(0) |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to move this logic (that is repeated quite a few times) into a function?