Skip to content

Commit a1888f5

Browse files
committed
- Fixed forward stream seeking emulation in streams that don't support seeking
in situations where the read operation gives back less data than requested and when there was data in the buffer before the emulation started. Also made more consistent its behavior -- should return failure every time less data than was requested was skipped. - Small performance improvement by correcting off-by-one error that generate an invalid call to the seek handler or read handler. in _php_stream_seek.
1 parent 890d89f commit a1888f5

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

main/streams/streams.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,16 +1097,16 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_
10971097
if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) {
10981098
switch(whence) {
10991099
case SEEK_CUR:
1100-
if (offset > 0 && offset < stream->writepos - stream->readpos) {
1101-
stream->readpos += offset;
1100+
if (offset > 0 && offset <= stream->writepos - stream->readpos) {
1101+
stream->readpos += offset; /* if offset = ..., then readpos = writepos */
11021102
stream->position += offset;
11031103
stream->eof = 0;
11041104
return 0;
11051105
}
11061106
break;
11071107
case SEEK_SET:
11081108
if (offset > stream->position &&
1109-
offset < stream->position + stream->writepos - stream->readpos) {
1109+
offset <= stream->position + stream->writepos - stream->readpos) {
11101110
stream->readpos += offset - stream->position;
11111111
stream->position = offset;
11121112
stream->eof = 0;
@@ -1149,14 +1149,12 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_
11491149
/* emulate forward moving seeks with reads */
11501150
if (whence == SEEK_CUR && offset > 0) {
11511151
char tmp[1024];
1152-
while(offset >= sizeof(tmp)) {
1153-
if (php_stream_read(stream, tmp, sizeof(tmp)) == 0) {
1152+
size_t didread;
1153+
while(offset > 0) {
1154+
if ((didread = php_stream_read(stream, tmp, MIN(offset, sizeof(tmp)))) == 0) {
11541155
return -1;
11551156
}
1156-
offset -= sizeof(tmp);
1157-
}
1158-
if (offset && (php_stream_read(stream, tmp, offset) == 0)) {
1159-
return -1;
1157+
offset -= didread;
11601158
}
11611159
stream->eof = 0;
11621160
return 0;

0 commit comments

Comments
 (0)