Skip to content

Commit 2e47442

Browse files
committed
Fix phpGH-18212: fseek with SEEK_CUR and negative offset crash on debug
Triggers the assertion as with SEEK_CUR the stream position is set to a negative value so we force the failure without affecting its position instead. close phpGH-18224
1 parent a21065e commit 2e47442

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ PHP NEWS
1010
. Fixed bug GH-18145 (php8ts crashes in php_clear_stat_cache()).
1111
(Jakub Zelenka)
1212
. Fixed bug GH-18209 (Use-after-free in extract() with EXTR_REFS). (ilutov)
13+
. Fixed bug GH-18212 (fseek with SEEK_CUR whence value and negative offset
14+
leads to negative stream position). (David Carlier)
1315

1416
10 Apr 2025, PHP 8.3.20
1517

ext/standard/tests/file/gh18212.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
GH-18212: fseek with SEEK_CUR and negative offset leads to negative file stream position.
3+
--FILE--
4+
<?php
5+
$fp = fopen('php://input', 'r+');
6+
var_dump(fseek($fp, -1, SEEK_SET));
7+
var_dump(fseek($fp, -32, SEEK_CUR));
8+
fclose($fp);
9+
?>
10+
--EXPECT--
11+
int(-1)
12+
int(-1)
13+

ext/standard/tests/file/stream_rfc2397_007.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int(2)
118118
bool(false)
119119
===S:-10,C===
120120
int(-1)
121-
bool(false)
121+
int(2)
122122
bool(false)
123123
===S:3,S===
124124
int(0)

main/streams/streams.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,10 @@ PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence)
13901390
}
13911391
whence = SEEK_SET;
13921392
break;
1393+
case SEEK_SET:
1394+
if (offset < 0) {
1395+
return -1;
1396+
}
13931397
}
13941398
ret = stream->ops->seek(stream, offset, whence, &stream->position);
13951399

0 commit comments

Comments
 (0)