Skip to content

Promote warnings to errors in dirname() #4600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ PHPAPI size_t php_dirname(char *path, size_t len)
}
/* }}} */

/* {{{ proto string|null dirname(string path[, int levels])
/* {{{ proto string dirname(string path[, int levels])
Returns the directory name component of the path */
PHP_FUNCTION(dirname)
{
Expand All @@ -1644,7 +1644,7 @@ PHP_FUNCTION(dirname)
ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len);
#endif
} else if (levels < 1) {
php_error_docref(NULL, E_WARNING, "Invalid argument, levels must be >= 1");
zend_throw_error(NULL, "Invalid argument, levels must be >= 1");
zend_string_efree(ret);
return;
} else {
Expand Down
10 changes: 6 additions & 4 deletions ext/standard/tests/strings/dirname_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ Test dirname() function : error conditions
echo "*** Testing error conditions ***\n";

// Bad arg
var_dump( dirname("/var/tmp/bar.gz", 0) );
try {
dirname("/var/tmp/bar.gz", 0);
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

echo "Done\n";
?>
--EXPECTF--
*** Testing error conditions ***

Warning: dirname(): Invalid argument, levels must be >= 1 in %s on line %d
NULL
Invalid argument, levels must be >= 1
Done
9 changes: 6 additions & 3 deletions ext/standard/tests/strings/dirname_multi.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ if((substr(PHP_OS, 0, 3) == "WIN"))
Description: Returns directory name component of path.
*/
for ($i=0 ; $i<5 ; $i++) {
var_dump(dirname("/foo/bar/baz", $i));
try {
var_dump(dirname("/foo/bar/baz", $i));
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
}
var_dump(dirname("/foo/bar/baz", PHP_INT_MAX));
?>
Done
--EXPECTF--
Warning: dirname(): Invalid argument, levels must be >= 1 in %sdirname_multi.php on line %d
NULL
Invalid argument, levels must be >= 1
string(8) "/foo/bar"
string(4) "/foo"
string(1) "/"
Expand Down
13 changes: 9 additions & 4 deletions ext/standard/tests/strings/dirname_multi_win.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ if((substr(PHP_OS, 0, 3) != "WIN"))
/* Prototype: string dirname ( string $path [, int nb]);
Description: Returns directory name component of path.
*/

for ($i=0 ; $i<5 ; $i++) {
var_dump(dirname("/foo/bar/baz", $i));
try {
var_dump(dirname("/foo/bar/baz", $i));
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}
}

var_dump(dirname("/foo/bar/baz", PHP_INT_MAX));
var_dump(dirname("g:/foo/bar/baz", PHP_INT_MAX));
var_dump(dirname("g:foo/bar/baz", PHP_INT_MAX));
?>
Done
--EXPECTF--
Warning: dirname(): Invalid argument, levels must be >= 1 in %sdirname_multi_win.php on line %d
NULL
--EXPECT--
Invalid argument, levels must be >= 1
string(8) "/foo/bar"
string(4) "/foo"
string(1) "\"
Expand Down