Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,11 @@ PHP_FUNCTION(mkdir)
Z_PARAM_RESOURCE_OR_NULL(zcontext)
ZEND_PARSE_PARAMETERS_END();

if (mode < 0 || (mode & ~07777)) {
zend_argument_value_error(2, "must be between 0 and 0o7777");
RETURN_THROWS();
}

context = php_stream_context_from_zval(zcontext, 0);

RETURN_BOOL(php_stream_mkdir(dir, (int)mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context));
Expand Down
34 changes: 34 additions & 0 deletions ext/standard/tests/file/mkdir_invalid_mode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
mkdir(): invalid mode
--FILE--
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add few more cases (e.g. -1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

$testCases = [
1000000, // way too large
-1, // negative
0o10000, // above 0o7777
0x1FFFF, // hex, still too large
12345, // arbitrary invalid
];

foreach ($testCases as $mode) {
try {
echo "Testing mode: $mode\n";
mkdir(__DIR__ . "/testdir_$mode", $mode);
} catch (ValueError $e) {
echo $e->getMessage(), PHP_EOL;
} catch (Exception $e) {
echo "Other exception: ", $e->getMessage(), PHP_EOL;
}
}
?>
--EXPECT--
Testing mode: 1000000
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: -1
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: 4096
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: 131071
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Testing mode: 12345
mkdir(): Argument #2 ($permissions) must be between 0 and 0o7777
Loading