Skip to content

Commit a407c34

Browse files
committed
Remove include "sanity check" to get better error
Instead of (silently!) rejecting directories / non-regular files early, we should just accept them and error later when a read is attempted. This is more general and will generate a proper error.
1 parent 1cbcf0f commit a407c34

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

‎Zend/tests/require_directory.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Including a directory generates an error
3+
--FILE--
4+
<?php
5+
6+
/* Just a random test directory */
7+
$dir = __DIR__ . '/variadic';
8+
require $dir;
9+
10+
?>
11+
--EXPECTF--
12+
Notice: require(): read of %d bytes failed with errno=21 Is a directory in %s on line %d
13+
14+
Fatal error: require(): Failed opening required '%s' (include_path='%s') in %s on line %d

‎Zend/zend_language_scanner.l

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ ZEND_API int open_file_for_scanning(zend_file_handle *file_handle)
517517
zend_string *compiled_filename;
518518

519519
if (zend_stream_fixup(file_handle, &buf, &size) == FAILURE) {
520+
/* Still add it to open_files so the file_handle destruction logic works correctly. */
521+
zend_llist_add_element(&CG(open_files), file_handle);
520522
return FAILURE;
521523
}
522524

‎main/streams/plain_wrapper.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ typedef struct {
128128
unsigned is_pipe:1; /* don't try and seek */
129129
unsigned cached_fstat:1; /* sb is valid */
130130
unsigned is_pipe_blocking:1; /* allow blocking read() on pipes, currently Windows only */
131-
unsigned no_forced_fstat:1; /* Use fstat cache even if forced */
132131
unsigned _reserved:28;
133132

134133
int lock_flag; /* stores the lock state */
@@ -153,7 +152,7 @@ typedef struct {
153152

154153
static int do_fstat(php_stdio_stream_data *d, int force)
155154
{
156-
if (!d->cached_fstat || (force && !d->no_forced_fstat)) {
155+
if (!d->cached_fstat || force) {
157156
int fd;
158157
int r;
159158

@@ -1084,30 +1083,7 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, zen
10841083
efree(persistent_id);
10851084
}
10861085

1087-
/* WIN32 always set ISREG flag */
10881086
#ifndef PHP_WIN32
1089-
/* sanity checks for include/require.
1090-
* We check these after opening the stream, so that we save
1091-
* on fstat() syscalls */
1092-
if (options & STREAM_OPEN_FOR_INCLUDE) {
1093-
php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
1094-
int r;
1095-
1096-
r = do_fstat(self, 0);
1097-
if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
1098-
if (opened_path) {
1099-
zend_string_release_ex(*opened_path, 0);
1100-
*opened_path = NULL;
1101-
}
1102-
php_stream_close(ret);
1103-
return NULL;
1104-
}
1105-
1106-
/* Make sure the fstat result is reused when we later try to get the
1107-
* file size. */
1108-
self->no_forced_fstat = 1;
1109-
}
1110-
11111087
if (options & STREAM_USE_BLOCKING_PIPE) {
11121088
php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
11131089
self->is_pipe_blocking = 1;

0 commit comments

Comments
 (0)