Skip to content

Commit 766cac0

Browse files
committed
Fix bug #76857: Can read "non-existant" files
This change makes checked and opened file consistent in a way that it is using real path for stat operation in the same way like it is used for open. Closes GH-12067
1 parent 4e963bc commit 766cac0

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ PHP NEWS
2323
- Streams:
2424
. Fixed bug #52335 (fseek() on memory stream behavior different than file).
2525
(Jakub Zelenka)
26+
. Fixed bug #76857 (Can read "non-existant" files). (Jakub Zelenka)
2627

2728
17 Aug 2023, PHP 8.3.0beta3
2829

UPGRADING

+3
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ PHP 8.3 UPGRADE NOTES
651651
. Memory stream no longer fails if seek offset is past the end. Instead
652652
the memory is increase on the next write and date between the old end and
653653
offset is filled with zero bytes in the same way how it works for files.
654+
. stat() access operartions like file_exists() and similar will now use real
655+
path instead of the actual stream path. This is consitent with stream
656+
opening.
654657

655658
========================================
656659
14. Performance Improvements

ext/standard/filestat.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -726,26 +726,29 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value)
726726
}
727727

728728
if (wrapper == &php_plain_files_wrapper) {
729-
729+
char realpath[MAXPATHLEN];
730+
if (expand_filepath(local, realpath) == NULL) {
731+
strlcpy(realpath, local, sizeof(realpath));
732+
}
730733
switch (type) {
731734
#ifdef F_OK
732735
case FS_EXISTS:
733-
RETURN_BOOL(VCWD_ACCESS(local, F_OK) == 0);
736+
RETURN_BOOL(VCWD_ACCESS(realpath, F_OK) == 0);
734737
break;
735738
#endif
736739
#ifdef W_OK
737740
case FS_IS_W:
738-
RETURN_BOOL(VCWD_ACCESS(local, W_OK) == 0);
741+
RETURN_BOOL(VCWD_ACCESS(realpath, W_OK) == 0);
739742
break;
740743
#endif
741744
#ifdef R_OK
742745
case FS_IS_R:
743-
RETURN_BOOL(VCWD_ACCESS(local, R_OK) == 0);
746+
RETURN_BOOL(VCWD_ACCESS(realpath, R_OK) == 0);
744747
break;
745748
#endif
746749
#ifdef X_OK
747750
case FS_IS_X:
748-
RETURN_BOOL(VCWD_ACCESS(local, X_OK) == 0);
751+
RETURN_BOOL(VCWD_ACCESS(realpath, X_OK) == 0);
749752
break;
750753
#endif
751754
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #76857 (Can read "non-existant" files)
3+
--FILE--
4+
<?php
5+
file_put_contents(__DIR__ . '/bug76857_data.txt', 'test data');
6+
$path = "foobar://google.com/../../bug76857_data.txt";
7+
chdir(__DIR__);
8+
var_dump(file_exists($path));
9+
var_dump(file_get_contents($path, false, null, 0, 10));
10+
?>
11+
--EXPECTF--
12+
Warning: file_exists(): Unable to find the wrapper "foobar" - did you forget to enable it when you configured PHP? in %s on line %d
13+
bool(true)
14+
15+
Warning: file_get_contents(): Unable to find the wrapper "foobar" - did you forget to enable it when you configured PHP? in %s on line %d
16+
string(9) "test data"
17+
--CLEAN--
18+
<?php
19+
@unlink(__DIR__ . '/bug76857_data.txt');
20+
?>

0 commit comments

Comments
 (0)