Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix GH-19570: unable to fseek in /dev/zero and /dev/null #19736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nielsdos wants to merge 1 commit into php:PHP-8.3
base: PHP-8.3
Choose a base branch
Loading
from nielsdos:fix-19570
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ext/standard/tests/streams/gh19570.phpt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-19570 (unable to fseek in /dev/zero and /dev/null)
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Linux") die("skip only for Linux");
?>
--FILE--
<?php
var_dump(fseek(fopen("/dev/zero", "rb"), 1*1024*1024, SEEK_SET));
var_dump(fseek(fopen("/dev/null", "rb"), 1*1024*1024, SEEK_SET));
?>
--EXPECT--
int(0)
int(0)
15 changes: 15 additions & 0 deletions main/streams/plain_wrapper.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
# include <limits.h>
#endif

#ifdef __linux__
# include <sys/sysmacros.h>
#endif

#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC)
#define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC)
#define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC)
Expand Down Expand Up @@ -255,7 +259,18 @@ PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC)
static void detect_is_seekable(php_stdio_stream_data *self) {
#if defined(S_ISFIFO) && defined(S_ISCHR)
if (self->fd >= 0 && do_fstat(self, 0) == 0) {
#ifdef __linux__
if (S_ISCHR(self->sb.st_mode)) {
Copy link
Member

@devnexen devnexen Sep 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that not just linux, allows some character devices being seekable, e.g. freebsd and very likely macos. The equivalent for freebsd at least, can t rely on device versions at all but on a "poorer" solution such as if fseek returns -1 and errno is ESPIPE then it is not seekable.

/* /dev/null & /dev/zero are exceptions, check their major/minor ID
* https://www.kernel.org/doc/Documentation/admin-guide/devices.txt */
self->is_seekable = major(self->sb.st_rdev) == 1
&& (minor(self->sb.st_rdev) == 3 || minor(self->sb.st_rdev) == 5);
} else {
self->is_seekable = !S_ISFIFO(self->sb.st_mode);
}
#else
self->is_seekable = !(S_ISFIFO(self->sb.st_mode) || S_ISCHR(self->sb.st_mode));
#endif
self->is_pipe = S_ISFIFO(self->sb.st_mode);
}
#elif defined(PHP_WIN32)
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /