Skip to content

Commit e3a5e42

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Preserve file-position when php://temp switches to temporary file
2 parents 8633893 + 84c18f9 commit e3a5e42

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ PHP NEWS
2121
. Fixed bug GH-8267 (MySQLi uses unsupported format specifier on Windows).
2222
(cmb)
2323

24+
- Streams:
25+
. Fixed php://temp does not preserve file-position when switched to temporary
26+
file. (Bernd Holzmüller)
27+
2428
14 Apr 2022, PHP 8.1.5
2529

2630
- Core:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
BUG: php://temp does not preserve file-pointer once it switches from memory to temporary file
3+
--FILE--
4+
<?php
5+
6+
$f = fopen('php://temp/maxmemory:1024', 'r+');
7+
fwrite($f, str_repeat("1", 738));
8+
fseek($f, 0, SEEK_SET);
9+
fwrite($f, str_repeat("2", 512));
10+
11+
fseek($f, 0, SEEK_SET);
12+
var_dump(fread($f, 16));
13+
14+
fseek($f, 0, SEEK_END);
15+
var_dump(ftell($f));
16+
17+
?>
18+
--EXPECT--
19+
string(16) "2222222222222222"
20+
int(738)

main/streams/memory.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,10 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t
346346
return -1;
347347
}
348348
if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
349-
zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
350-
351-
if (ZSTR_LEN(membuf) + count >= ts->smax) {
349+
zend_off_t pos = php_stream_tell(ts->innerstream);
350+
351+
if (pos + count >= ts->smax) {
352+
zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
352353
php_stream *file = php_stream_fopen_temporary_file(ts->tmpdir, "php", NULL);
353354
if (file == NULL) {
354355
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
358359
php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
359360
ts->innerstream = file;
360361
php_stream_encloses(stream, ts->innerstream);
362+
php_stream_seek(ts->innerstream, pos, SEEK_SET);
361363
}
362364
}
363365
return php_stream_write(ts->innerstream, buf, count);

0 commit comments

Comments
 (0)