Skip to content

Commit 3b0e619

Browse files
divinity76iluuu1994
authored andcommitted
fix file() flags error-check
the old flag check was flawed and would miss some flags, for example: file(__FILE__, FILE_APPEND); is invalid, but the old flags error check would miss it: https://3v4l.org/b2W9u Closes phpGH-11483
1 parent 3c87266 commit 3b0e619

File tree

4 files changed

+26
-77
lines changed

4 files changed

+26
-77
lines changed

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ PHP 8.3 UPGRADE NOTES
6868
* range() now produce a list of characters if one of the boundary inputs is
6969
a string digit instead of casting the other input to int
7070
(e.g. range('5', 'z');)
71+
. The file() flags error check now catches all invalid flags. Notably
72+
FILE_APPEND was previously silently accepted.
7173

7274
========================================
7375
2. New Features
@@ -355,3 +357,6 @@ PHP 8.3 UPGRADE NOTES
355357
========================================
356358
14. Performance Improvements
357359
========================================
360+
361+
- Standard:
362+
. The file() flags error check is now about 7% faster.

ext/standard/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ PHP_FUNCTION(file)
619619
Z_PARAM_RESOURCE_OR_NULL(zcontext)
620620
ZEND_PARSE_PARAMETERS_END();
621621

622-
if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
622+
if ((flags & ~(PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) != 0) {
623623
zend_argument_value_error(2, "must be a valid flag value");
624624
RETURN_THROWS();
625625
}

ext/standard/tests/file/file_error.phpt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ echo "\n*** Testing error conditions ***\n";
77
$file_handle = fopen($file_path."/file.tmp", "w");
88

99
$filename = $file_path."/file.tmp";
10-
var_dump( file($filename, 10, NULL) ); // Incorrect flag
11-
10+
try {
11+
var_dump( file($filename, 10, NULL) ); // Incorrect flag
12+
} catch(ValueError $e) {
13+
echo "ValueError: " . $e->getMessage() . "\n";
14+
}
15+
try {
16+
var_dump( file($filename, FILE_APPEND) ); // Incorrect flag
17+
} catch(ValueError $e) {
18+
echo "ValueError: " . $e->getMessage() . "\n";
19+
}
1220
var_dump( file("temp.tmp") ); // non existing filename
1321
fclose($file_handle);
1422

@@ -21,8 +29,8 @@ unlink($file_path."/file.tmp");
2129
?>
2230
--EXPECTF--
2331
*** Testing error conditions ***
24-
array(0) {
25-
}
32+
ValueError: file(): Argument #2 ($flags) must be a valid flag value
33+
ValueError: file(): Argument #2 ($flags) must be a valid flag value
2634

2735
Warning: file(temp.tmp): Failed to open stream: No such file or directory in %s on line %d
2836
bool(false)

ext/standard/tests/file/file_variation6.phpt

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -95,78 +95,14 @@ array(3) {
9595
[2]=>
9696
string(6) "Line 3"
9797
}
98-
array(3) {
99-
[0]=>
100-
string(7) "Line 1
101-
"
102-
[1]=>
103-
string(7) "Line 2
104-
"
105-
[2]=>
106-
string(6) "Line 3"
107-
}
108-
array(3) {
109-
[0]=>
110-
string(7) "Line 1
111-
"
112-
[1]=>
113-
string(7) "Line 2
114-
"
115-
[2]=>
116-
string(6) "Line 3"
117-
}
118-
array(3) {
119-
[0]=>
120-
string(6) "Line 1"
121-
[1]=>
122-
string(6) "Line 2"
123-
[2]=>
124-
string(6) "Line 3"
125-
}
126-
array(3) {
127-
[0]=>
128-
string(6) "Line 1"
129-
[1]=>
130-
string(6) "Line 2"
131-
[2]=>
132-
string(6) "Line 3"
133-
}
134-
array(3) {
135-
[0]=>
136-
string(7) "Line 1
137-
"
138-
[1]=>
139-
string(7) "Line 2
140-
"
141-
[2]=>
142-
string(6) "Line 3"
143-
}
144-
array(3) {
145-
[0]=>
146-
string(7) "Line 1
147-
"
148-
[1]=>
149-
string(7) "Line 2
150-
"
151-
[2]=>
152-
string(6) "Line 3"
153-
}
154-
array(3) {
155-
[0]=>
156-
string(6) "Line 1"
157-
[1]=>
158-
string(6) "Line 2"
159-
[2]=>
160-
string(6) "Line 3"
161-
}
162-
array(3) {
163-
[0]=>
164-
string(6) "Line 1"
165-
[1]=>
166-
string(6) "Line 2"
167-
[2]=>
168-
string(6) "Line 3"
169-
}
98+
file(): Argument #2 ($flags) must be a valid flag value
99+
file(): Argument #2 ($flags) must be a valid flag value
100+
file(): Argument #2 ($flags) must be a valid flag value
101+
file(): Argument #2 ($flags) must be a valid flag value
102+
file(): Argument #2 ($flags) must be a valid flag value
103+
file(): Argument #2 ($flags) must be a valid flag value
104+
file(): Argument #2 ($flags) must be a valid flag value
105+
file(): Argument #2 ($flags) must be a valid flag value
170106
array(3) {
171107
[0]=>
172108
string(7) "Line 1

0 commit comments

Comments
 (0)