Skip to content

Commit 6a04c79

Browse files
committed
Fix GH-15980: Signed integer overflow in main/streams/streams.c
We need to avoid signed integer overflows which are undefined behavior. We catch that, and set `offset` to `ZEND_LONG_MAX` (which is also the largest value of `zend_off_t` on all platforms). Of course, after such a seek a stream is no longer readable, but that matches the current behavior for offsets near `ZEND_LONG_MAX`. Closes GH-15989.
1 parent f303840 commit 6a04c79

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ PHP NEWS
2929
- Streams:
3030
. Fixed bugs GH-15908 and GH-15026 (leak / assertion failure in streams.c).
3131
(nielsdos)
32+
. Fixed bug GH-15980 (Signed integer overflow in main/streams/streams.c).
33+
(cmb)
3234

3335
- TSRM:
3436
. Prevent closing of unrelated handles. (cmb)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
GH-15980 (Signed integer overflow in main/streams/streams.c)
3+
--FILE--
4+
<?php
5+
$s = fopen(__FILE__, "r");
6+
fseek($s, 1);
7+
fseek($s, PHP_INT_MAX, SEEK_CUR);
8+
var_dump(ftell($s) > 1);
9+
?>
10+
--EXPECT--
11+
bool(true)

main/streams/streams.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,13 @@ PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence)
13541354

13551355
switch(whence) {
13561356
case SEEK_CUR:
1357-
offset = stream->position + offset;
1358-
whence = SEEK_SET;
1357+
ZEND_ASSERT(stream->position >= 0);
1358+
if (UNEXPECTED(offset > ZEND_LONG_MAX - stream->position)) {
1359+
offset = ZEND_LONG_MAX;
1360+
} else {
1361+
offset = stream->position + offset;
1362+
}
1363+
whence = SEEK_SET;
13591364
break;
13601365
}
13611366
ret = stream->ops->seek(stream, offset, whence, &stream->position);

0 commit comments

Comments
 (0)