Commit 497318f0e65 for php
commit 497318f0e65d527e617961cdb7a3a91293dcb303
Author: David Carlier <devnexen@gmail.com>
Date: Fri Apr 10 05:58:23 2026 +0100
streams: _php_stream_seek() assertion triggered on negative offset.
Fix GH-21700
Close GH-21701
diff --git a/NEWS b/NEWS
index 2d97165c2ec..83eabf69b36 100644
--- a/NEWS
+++ b/NEWS
@@ -31,6 +31,10 @@ PHP NEWS
. Fixed array_udiff_assoc() and array_uintersect_assoc() clobbering the user
comparison callback. (lacatoire)
+- Streams:
+ . Fixed bug GH-21700 (_php_stream_seek() assertion triggered on negative
+ offset). (David Carlier)
+
- Sysvmsg:
. Fixed msg_receive() reading past the end of the received message when
unserializing it. (Sebastian Bergmann)
diff --git a/ext/standard/tests/streams/gh21700.phpt b/ext/standard/tests/streams/gh21700.phpt
new file mode 100644
index 00000000000..a6703238096
--- /dev/null
+++ b/ext/standard/tests/streams/gh21700.phpt
@@ -0,0 +1,22 @@
+--TEST--
+GH-21700 (_php_stream_seek assertion on SEEK_CUR with resolved negative offset)
+--CREDITS--
+YuanchengJiang
+--FILE--
+<?php
+class wrapper {
+ public int $pos = 0;
+ public $context;
+ function stream_open($path, $mode, $options, &$opened_path): bool { return true; }
+ function stream_seek($offset, $whence): bool { $this->pos = $offset; return true; }
+ function stream_tell(): int { return $this->pos; }
+}
+stream_wrapper_register("gh21700", "wrapper");
+$fp = fopen("gh21700://", "rb");
+var_dump(fseek($fp, -1, SEEK_CUR));
+var_dump(fseek($fp, 0, SEEK_CUR));
+fclose($fp);
+?>
+--EXPECT--
+int(-1)
+int(0)
diff --git a/main/streams/streams.c b/main/streams/streams.c
index 4c3ce0ec1b8..b887f4ef518 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -1469,7 +1469,8 @@ PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence)
offset = stream->position + offset;
}
whence = SEEK_SET;
- break;
+ /* fall through to reject a possible negative offset */
+ ZEND_FALLTHROUGH;
case SEEK_SET:
if (offset < 0) {
return -1;