php://temp does not preserve file-position when switched to temporary file #8333
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.
This is my first PR for PHP. Please forgive me if I violated some rules that I didn't know so far or understood wrong while reading your guidelines.
I discovered a small flaw in PHP's php://temp stream-wrapper:
The stream is kept in memory until a given amount of data was written to it. Once this limit is reached during
fwrite()
PHP will offload the stream to a temporary file.PHP detects whether the limit is reached by adding the length to be written to the length of the actual stream. This is right as long as the file-position is at the end of the stream. If the pointer is somewhere else, there might be no need to switch to a temporary file at all. Plus: The file-position is not preserved during the switch so contents will be written to the end of the file regardless of the previous file-position.
I wrote a very small test for this bug that may explain the issue better than my words. I also included a fix for the issue itself.
Thank you in advance!