Skip to content

Commit 3943351

Browse files
committed
- [DOC] Reverted rev #304382 and rev #304380, as I figured out a way to
fix the erratic behavior without breaking backwards compatibility. Namely, $offset retains SEEK_SET behavior but actually SEEK_CUR is passed to _php_stream_seek, if possible, by moving the offset stream->position bytes. - Addresses bug #53006.
1 parent fbd3eb6 commit 3943351

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

ext/standard/streamsfuncs.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ PHP_FUNCTION(stream_get_contents)
415415
{
416416
php_stream *stream;
417417
zval *zsrc;
418-
long maxlen = PHP_STREAM_COPY_ALL, pos = 0;
418+
long maxlen = PHP_STREAM_COPY_ALL, pos = -1L;
419419
int len, newlen;
420420
char *contents = NULL;
421421

@@ -425,12 +425,19 @@ PHP_FUNCTION(stream_get_contents)
425425

426426
php_stream_from_zval(stream, &zsrc);
427427

428-
if ((pos > 0L) && php_stream_seek(stream, pos, SEEK_CUR) < 0) {
429-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek %ld bytes from current position in the stream", pos);
430-
RETURN_FALSE;
431-
} else if (pos < 0L) {
432-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of bytes to seek must be non-negative, given %ld", pos);
433-
RETURN_FALSE;
428+
if (pos >= 0) {
429+
int seek_res = 0;
430+
if (pos > stream->position) {
431+
/* use SEEK_CUR to allow emulation in streams that don't support seeking */
432+
seek_res = php_stream_seek(stream, pos - stream->position, SEEK_CUR);
433+
} else if (pos < stream->position) {
434+
seek_res = php_stream_seek(stream, pos, SEEK_SET);
435+
}
436+
437+
if (seek_res != 0) {
438+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to position %ld in the stream", pos);
439+
RETURN_FALSE;
440+
}
434441
}
435442

436443
len = php_stream_copy_to_mem(stream, &contents, maxlen, 0);

ext/standard/tests/streams/bug46426.phpt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,27 @@ $tmp = tmpfile();
77

88
fwrite($tmp, b"12345");
99

10-
fseek($tmp, 0);
11-
echo stream_get_contents($tmp, 2, 1); //23
10+
echo stream_get_contents($tmp, 2, 1);
1211
echo "\n";
13-
echo stream_get_contents($tmp, -1); //45
12+
echo stream_get_contents($tmp, -1);
1413
echo "\n";
15-
fseek($tmp, -1, SEEK_CUR);
16-
echo stream_get_contents($tmp, -1, 0); //5
14+
echo stream_get_contents($tmp, -1, 0);
1715
echo "\n";
18-
fseek($tmp, 0);
19-
echo stream_get_contents($tmp, -1, 2); //345
16+
echo stream_get_contents($tmp, -1, 2);
2017
echo "\n";
21-
fseek($tmp, 0);
22-
echo stream_get_contents($tmp, 0, 0); //""
18+
echo stream_get_contents($tmp, 0, 0);
2319
echo "\n";
24-
echo stream_get_contents($tmp, 1, 0); //1
20+
echo stream_get_contents($tmp, 1, 0);
2521
echo "\n";
26-
echo stream_get_contents($tmp, -1); //2345
22+
echo stream_get_contents($tmp, -1);
2723

2824
@unlink($tmp);
2925

3026
?>
3127
--EXPECT--
3228
23
3329
45
34-
5
30+
12345
3531
345
3632

3733
1

ext/standard/tests/streams/stream_get_contents_001.phpt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ fwrite($tmp, b"12345");
99

1010
echo stream_get_contents($tmp, 2, 5), "--\n";
1111
echo stream_get_contents($tmp, 2), "--\n";
12-
fseek($tmp, 0);
1312
echo stream_get_contents($tmp, 2, 3), "--\n";
1413
echo stream_get_contents($tmp, 2, -1), "--\n";
1514

1615
@unlink($tmp);
1716

1817
?>
19-
--EXPECTF--
18+
--EXPECT--
2019
--
2120
--
2221
45--
23-
24-
Warning: stream_get_contents(): Number of bytes to seek must be non-negative, given -1 in %s on line %d
2522
--

0 commit comments

Comments
 (0)