Skip to content

Commit 30b2398

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Preserve file-position when php://temp switches to temporary file
2 parents 106c908 + e3a5e42 commit 30b2398

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
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
@@ -348,9 +348,10 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t
348348
return -1;
349349
}
350350
if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
351-
zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
352-
353-
if (ZSTR_LEN(membuf) + count >= ts->smax) {
351+
zend_off_t pos = php_stream_tell(ts->innerstream);
352+
353+
if (pos + count >= ts->smax) {
354+
zend_string *membuf = php_stream_memory_get_buffer(ts->innerstream);
354355
php_stream *file = php_stream_fopen_temporary_file(ts->tmpdir, "php", NULL);
355356
if (file == NULL) {
356357
php_error_docref(NULL, E_WARNING, "Unable to create temporary file, Check permissions in temporary files directory.");
@@ -360,6 +361,7 @@ static ssize_t php_stream_temp_write(php_stream *stream, const char *buf, size_t
360361
php_stream_free_enclosed(ts->innerstream, PHP_STREAM_FREE_CLOSE);
361362
ts->innerstream = file;
362363
php_stream_encloses(stream, ts->innerstream);
364+
php_stream_seek(ts->innerstream, pos, SEEK_SET);
363365
}
364366
}
365367
return php_stream_write(ts->innerstream, buf, count);

0 commit comments

Comments
 (0)