Merge branch 'PHP-8.0' into PHP-8.1

* PHP-8.0:
  Preserve file-position when php://temp switches to temporary file
This commit is contained in:
Christoph M. Becker 2022-04-11 12:35:06 +02:00
commit e3a5e424f6
No known key found for this signature in database
GPG Key ID: D66C9593118BCCB6
3 changed files with 29 additions and 3 deletions

4
NEWS
View File

@ -21,6 +21,10 @@ PHP NEWS
. Fixed bug GH-8267 (MySQLi uses unsupported format specifier on Windows).
(cmb)
- Streams:
. Fixed php://temp does not preserve file-position when switched to temporary
file. (Bernd Holzmüller)
14 Apr 2022, PHP 8.1.5
- Core:

View File

@ -0,0 +1,20 @@
--TEST--
BUG: php://temp does not preserve file-pointer once it switches from memory to temporary file
--FILE--
<?php
$f = fopen('php://temp/maxmemory:1024', 'r+');
fwrite($f, str_repeat("1", 738));
fseek($f, 0, SEEK_SET);
fwrite($f, str_repeat("2", 512));
fseek($f, 0, SEEK_SET);
var_dump(fread($f, 16));
fseek($f, 0, SEEK_END);
var_dump(ftell($f));
?>
--EXPECT--
string(16) "2222222222222222"
int(738)

View File

@ -346,9 +346,10 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t
return -1;
}
if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
if (ZSTR_LEN(membuf) + count >= ts->smax) {
zend_off_t pos = php_stream_tell(ts->innerstream);
if (pos + count >= ts->smax) {
zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
php_stream *file = php_stream_fopen_temporary_file(ts->tmpdir, "php", NULL);
if (file == NULL) {
php_error_docref(NULL, E_WARNING, "Unable to create temporary file, Check permissions in temporary files directory.");
@ -358,6 +359,7 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t
php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
ts->innerstream = file;
php_stream_encloses(stream, ts->innerstream);
php_stream_seek(ts->innerstream, pos, SEEK_SET);
}
}
return php_stream_write(ts->innerstream, buf, count);