From d3a8b916f6b6c519e9d228030e6338842aaf5b1d Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 27 Jul 2020 16:15:16 +0100 Subject: [PATCH] Example case with dir functions --- ext/standard/dir.c | 13 ++++--- ...scandir_error2_throw_on_error_declare.phpt | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 ext/standard/tests/dir/scandir_error2_throw_on_error_declare.phpt diff --git a/ext/standard/dir.c b/ext/standard/dir.c index 7d3af076977c..2a030cf2cc5b 100644 --- a/ext/standard/dir.c +++ b/ext/standard/dir.c @@ -23,6 +23,7 @@ #include "php_string.h" #include "php_scandir.h" #include "basic_functions.h" +#include "io_exceptions.h" #include "dir_arginfo.h" #if HAVE_UNISTD_H @@ -278,7 +279,7 @@ PHP_FUNCTION(chroot) ret = chroot(str); if (ret != 0) { - php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno); + php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno); RETURN_FALSE; } @@ -287,7 +288,7 @@ PHP_FUNCTION(chroot) ret = chdir("/"); if (ret != 0) { - php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno); + php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno); RETURN_FALSE; } @@ -313,7 +314,7 @@ PHP_FUNCTION(chdir) ret = VCWD_CHDIR(str); if (ret != 0) { - php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno); + php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno); RETURN_FALSE; } @@ -415,12 +416,12 @@ PHP_FUNCTION(glob) ZEND_PARSE_PARAMETERS_END(); if (pattern_len >= MAXPATHLEN) { - php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN); + php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN); RETURN_FALSE; } if ((GLOB_AVAILABLE_FLAGS & flags) != flags) { - php_error_docref(NULL, E_WARNING, "At least one of the passed flags is invalid or not supported on this platform"); + php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "At least one of the passed flags is invalid or not supported on this platform"); RETURN_FALSE; } @@ -558,7 +559,7 @@ PHP_FUNCTION(scandir) n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr); } if (n < 0) { - php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno)); + php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "(errno %d): %s", errno, strerror(errno)); RETURN_FALSE; } diff --git a/ext/standard/tests/dir/scandir_error2_throw_on_error_declare.phpt b/ext/standard/tests/dir/scandir_error2_throw_on_error_declare.phpt new file mode 100644 index 000000000000..0e24aa54ce6a --- /dev/null +++ b/ext/standard/tests/dir/scandir_error2_throw_on_error_declare.phpt @@ -0,0 +1,38 @@ +--TEST-- +Test scandir() function error conditions with throw on error declare enabled - Non-existent directory +--SKIPIF-- + +--FILE-- +getMessage() . \PHP_EOL; +} + +echo "\n-- Pass scandir() a relative path that does not exist --\n"; +try { + var_dump(scandir('/idonotexist')); +} catch (\FileSystem $e) { + echo $e->getMessage() . \PHP_EOL; +} +?> +--EXPECT-- +*** Testing scandir() : error conditions *** + +-- Pass scandir() an absolute path that does not exist -- +(errno 2): No such file or directory + +-- Pass scandir() a relative path that does not exist -- +(errno 2): No such file or directory