-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
+29
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.