Fix GH-17399: iconv memory leak with large line-length#12
Closed
Fix GH-17399: iconv memory leak with large line-length#12
Conversation
Move the buf allocation in _php_iconv_mime_encode() before the iconv_open() calls. When max_line_len is excessively large (e.g. PHP_INT_MAX), safe_emalloc triggers an OOM bailout that skips cleanup, leaking the iconv handles allocated via system malloc. By allocating buf first, a bailout happens before any iconv handles exist. Closes phpGH-17399
Owner
Author
|
Submitted upstream as php#21541 |
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.
Summary
_php_iconv_mime_encode()opens twoiconv_thandles via systemmalloc, then callssafe_emalloc(1, max_line_len, 5). Whenmax_line_lenisPHP_INT_MAX, the allocation triggers an OOM bailout that skipsiconv_close()cleanup, leaking both handles.Fix: move
safe_emallocbefore bothiconv_open()calls. If it bails out, no handles exist to leak.@cmb69 suggested this approach in the issue thread.
Test plan
ext/iconv/tests/gh17399.phpttriggers the OOM pathext/iconv/tests/suite passes with zero regressions