-
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
Conversation
|
|
| *ptr = '\0'; | ||
| ZSTR_LEN(result) = len; | ||
| result = zend_string_truncate(result, len, persistent); | ||
| ZSTR_VAL(result)[len] = '\0'; |
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.
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.
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.
@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?
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.
done!
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.
This dropped the ZSTR_LEN(result) = len assignment, which is still needed in case we don't truncate. I'm surprised this doesn't cause test failures.
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.
@nikic you're right, it didn't I'm afraid, any ideas for a test that maybe we could add?
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.
fixed in any case
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.
<?php
$f = fopen('php://memory', 'rw');
fwrite($f, str_repeat('X', 1000));
fseek($f, 0);
var_dump(strlen(stream_get_contents($f, 1024)));
should show the issue.
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.
@nikic it did, thanks! I added the additional test just to be on the safe side
cc82d91 to
42ddb03
Compare
|
Should I also include a NEWS.txt entry? |
No. This is supposed to be done by the "merger". |
… fixed length buffer
|
Merged as dc7aa22 into 7.2+. Thanks! |
Hi!
This is my first contribution, all tests keep passing, but I'm not sure the fix is 100% correct. Thanks!!
Fixes https://bugs.php.net/bug.php?id=78326