Browse Source

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

On Linux, these two character devices are exceptions in that they can be
seeked. Check their major/minor device number.

Co-authored-by: divinity76 <hans@loltek.net>
pull/20082/head
Niels Dossche 1 month ago
parent
commit
b7aeb0a69f
No known key found for this signature in database GPG Key ID: B8A8AD166DF0E2E5
  1. 2
      NEWS
  2. 22
      ext/standard/tests/streams/gh19570.phpt
  3. 25
      main/streams/plain_wrapper.c

2
NEWS

@ -64,6 +64,8 @@ PHP NEWS
causing an exception on sort). (nielsdos)
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
while COW violation flag is still set). (alexandre-daubois)
. Fixed bug GH-19570 (unable to fseek in /dev/zero and /dev/null).
(nielsdos, divinity76)
- Streams:
. Fixed bug GH-19248 (Use strerror_r instead of strerror in main).

22
ext/standard/tests/streams/gh19570.phpt

@ -0,0 +1,22 @@
--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
$devices = [
// Note: could test others but they're not necessarily available
"/dev/zero",
"/dev/null",
"/dev/full",
];
foreach ($devices as $device) {
var_dump(fseek(fopen($device, "rb"), 1*1024*1024, SEEK_SET));
}
?>
--EXPECT--
int(0)
int(0)
int(0)

25
main/streams/plain_wrapper.c

@ -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,28 @@ 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)) {
/* Some character devices are exceptions, check their major/minor ID
* https://www.kernel.org/doc/Documentation/admin-guide/devices.txt */
if (major(self->sb.st_rdev) == 1) {
unsigned m = minor(self->sb.st_rdev);
self->is_seekable =
m == 1 || /* /dev/mem */
m == 2 || /* /dev/kmem */
m == 3 || /* /dev/null */
m == 4 || /* /dev/port (seekable, offset = I/O port) */
m == 5 || /* /dev/zero */
m == 7; /* /dev/full */
} else {
self->is_seekable = false;
}
} 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)

Loading…
Cancel
Save