-
Notifications
You must be signed in to change notification settings - Fork 8k
fix #78326 memory issues with stream_get_contents() fixed length buffer #4464
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --TEST-- | ||
| memory allocation on stream_get_contents() | ||
| --INI-- | ||
| memory_limit=32M | ||
| --FILE-- | ||
| <?php | ||
| $f = tmpfile(); | ||
| fwrite($f, '.'); | ||
|
|
||
| $chunks = array(); | ||
| for ($i = 0; $i < 1000; ++$i) { | ||
| rewind($f); | ||
| $chunks[] = stream_get_contents($f, 1000000); | ||
| } | ||
| var_dump(count($chunks)); | ||
| ?> | ||
| --EXPECT-- | ||
| int(1000) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --TEST-- | ||
| proper string length on stream_get_contents() | ||
| --FILE-- | ||
| <?php | ||
| $f = fopen('php://memory', 'rw'); | ||
| fwrite($f, str_repeat('X', 1000)); | ||
| fseek($f, 0); | ||
| var_dump(strlen(stream_get_contents($f, 1024))); | ||
| --EXPECT-- | ||
| int(1000) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1418,8 +1418,13 @@ PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int | |
| ptr += ret; | ||
| } | ||
| if (len) { | ||
| *ptr = '\0'; | ||
| ZSTR_LEN(result) = len; | ||
| ZSTR_VAL(result)[len] = '\0'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to use the same logic as in https://github.com/php/php-src/blob/master/main/streams/streams.c#L759 and only truncate if the "wastage" is large enough.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikic thanks! I guess that the potential savings of the truncate are not worth the extra performance costs of the function and memory copy calls if the "waste" is not large enough?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This dropped the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikic you're right, it didn't I'm afraid, any ideas for a test that maybe we could add?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in any case
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should show the issue.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nikic it did, thanks! I added the additional test just to be on the safe side |
||
|
|
||
| /* Only truncate if the savings are large enough */ | ||
| if (len < maxlen / 2) { | ||
| result = zend_string_truncate(result, len, persistent); | ||
| } | ||
| } else { | ||
| zend_string_free(result); | ||
| result = NULL; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.