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 GH-11483
This commit is contained in:
hanshenrik 2023-06-19 18:46:05 +02:00 committed by Ilija Tovilo
parent 3c872661c1
commit 3b0e61956e
No known key found for this signature in database
GPG Key ID: A4F5D403F118200A
4 changed files with 26 additions and 77 deletions

View File

@ -68,6 +68,8 @@ PHP 8.3 UPGRADE NOTES
* range() now produce a list of characters if one of the boundary inputs is
a string digit instead of casting the other input to int
(e.g. range('5', 'z');)
. The file() flags error check now catches all invalid flags. Notably
FILE_APPEND was previously silently accepted.
========================================
2. New Features
@ -355,3 +357,6 @@ PHP 8.3 UPGRADE NOTES
========================================
14. Performance Improvements
========================================
- Standard:
. The file() flags error check is now about 7% faster.

View File

@ -619,7 +619,7 @@ PHP_FUNCTION(file)
Z_PARAM_RESOURCE_OR_NULL(zcontext)
ZEND_PARSE_PARAMETERS_END();
if (flags < 0 || flags > (PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) {
if ((flags & ~(PHP_FILE_USE_INCLUDE_PATH | PHP_FILE_IGNORE_NEW_LINES | PHP_FILE_SKIP_EMPTY_LINES | PHP_FILE_NO_DEFAULT_CONTEXT)) != 0) {
zend_argument_value_error(2, "must be a valid flag value");
RETURN_THROWS();
}

View File

@ -7,8 +7,16 @@ echo "\n*** Testing error conditions ***\n";
$file_handle = fopen($file_path."/file.tmp", "w");
$filename = $file_path."/file.tmp";
var_dump( file($filename, 10, NULL) ); // Incorrect flag
try {
var_dump( file($filename, 10, NULL) ); // Incorrect flag
} catch(ValueError $e) {
echo "ValueError: " . $e->getMessage() . "\n";
}
try {
var_dump( file($filename, FILE_APPEND) ); // Incorrect flag
} catch(ValueError $e) {
echo "ValueError: " . $e->getMessage() . "\n";
}
var_dump( file("temp.tmp") ); // non existing filename
fclose($file_handle);
@ -21,8 +29,8 @@ unlink($file_path."/file.tmp");
?>
--EXPECTF--
*** Testing error conditions ***
array(0) {
}
ValueError: file(): Argument #2 ($flags) must be a valid flag value
ValueError: file(): Argument #2 ($flags) must be a valid flag value
Warning: file(temp.tmp): Failed to open stream: No such file or directory in %s on line %d
bool(false)

View File

@ -95,78 +95,14 @@ array(3) {
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(7) "Line 1
"
[1]=>
string(7) "Line 2
"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(7) "Line 1
"
[1]=>
string(7) "Line 2
"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(6) "Line 1"
[1]=>
string(6) "Line 2"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(6) "Line 1"
[1]=>
string(6) "Line 2"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(7) "Line 1
"
[1]=>
string(7) "Line 2
"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(7) "Line 1
"
[1]=>
string(7) "Line 2
"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(6) "Line 1"
[1]=>
string(6) "Line 2"
[2]=>
string(6) "Line 3"
}
array(3) {
[0]=>
string(6) "Line 1"
[1]=>
string(6) "Line 2"
[2]=>
string(6) "Line 3"
}
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
file(): Argument #2 ($flags) must be a valid flag value
array(3) {
[0]=>
string(7) "Line 1