Skip to content

Fix #80960: opendir() warning wrong info when failed on Windows #6872

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 4 commits 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
6 changes: 6 additions & 0 deletions Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,11 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
}
#endif
if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
#ifdef ZEND_WIN32
SET_ERRNO_FROM_WIN32_CODE(ERROR_BUFFER_OVERFLOW);
#else
errno = ENAMETOOLONG;
#endif
return 1;
}
memcpy(resolved_path, state->cwd, state_cwd_length);
Expand Down Expand Up @@ -1095,6 +1100,7 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
#ifdef ZEND_WIN32
if (memchr(resolved_path, '*', path_length) ||
memchr(resolved_path, '?', path_length)) {
SET_ERRNO_FROM_WIN32_CODE(ERROR_INVALID_NAME);
return 1;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion ext/phar/tests/phar_buildfromdirectory2-win.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ __HALT_COMPILER();
?>
--EXPECTF--
%s(24) "UnexpectedValueException"
RecursiveDirectoryIterator::__construct(1,1): %s (code: 2)
RecursiveDirectoryIterator::__construct(1): %s (code: 2)
===DONE===
2 changes: 1 addition & 1 deletion ext/phar/tests/phar_gobyebye-win32.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool(false)
bool(false)
bool(false)

Warning: opendir(foo/hi,foo/hi): %s (code: 3) in phar://%sphar_gobyebye-win32.phar.php/foo/hi on line %d
Warning: opendir(foo/hi): %s (code: 3) in phar://%sphar_gobyebye-win32.phar.php/foo/hi on line %d

Warning: opendir(foo/hi): failed to open dir: No such file or directory in phar://%sphar_gobyebye-win32.phar.php/foo/hi on line %d
===DONE===
26 changes: 26 additions & 0 deletions ext/standard/tests/dir/bug80960.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Fix #80960 (opendir() warning wrong info when failed on Windows)
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
?>
--INI--
log_errors_max_len=0
--FILE--
<?php
opendir("notexist*");
opendir("notexist?");
opendir(str_pad("longname", PHP_MAXPATHLEN - strlen(getcwd()), "_"));
?>
--EXPECTF--
Warning: opendir(notexist*): %s (code: 123) in %s on line %d

Warning: opendir(notexist*): failed to open dir: No such file or directory in %s on line %d

Warning: opendir(notexist?): %s (code: 123) in %s on line %d

Warning: opendir(notexist?): failed to open dir: No such file or directory in %s on line %d

Warning: opendir(longname%r_+%r): %s (code: 111) in %s on line %d

Warning: opendir(longname%r_+%r): failed to open dir: Filename too long in %s on line %d
8 changes: 4 additions & 4 deletions ext/standard/tests/dir/opendir_variation6-win32.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,24 @@ rmdir($dir_path);

-- Wildcard = '*' --

Warning: opendir(%s/opendir_var*,%s/opendir_var*): %s in %s on line %d
Warning: opendir(%s/opendir_var*): %s in %s on line %d

Warning: opendir(%s/opendir_var*): failed to open dir: %s in %s on line %d
bool(false)

Warning: opendir(%s/*,%s/*): %s in %s on line %d
Warning: opendir(%s/*): %s in %s on line %d

Warning: opendir(%s/*): failed to open dir: %s in %s on line %d
bool(false)

-- Wildcard = '?' --

Warning: opendir(%s/opendir_variation6/sub_dir?,%s/opendir_variation6/sub_dir?): %s in %s on line %d
Warning: opendir(%s/opendir_variation6/sub_dir?): %s in %s on line %d

Warning: opendir(%s/opendir_variation6/sub_dir?): failed to open dir: %s in %s on line %d
bool(false)

Warning: opendir(%s/opendir_variation6/sub?dir1,%s/opendir_variation6/sub?dir1): %s in %s on line %d
Warning: opendir(%s/opendir_variation6/sub?dir1): %s in %s on line %d

Warning: opendir(%s/opendir_variation6/sub?dir1): failed to open dir: %s in %s on line %d
bool(false)
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/general_functions/bug44295-win.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ try {
<?php exit(0); ?>
--EXPECTF--
before
in catch: DirectoryIterator::__construct(c:\not\exists\here,c:\not\exists\here): %s (code: 3)
in catch: DirectoryIterator::__construct(c:\not\exists\here): %s (code: 3)
==DONE==
13 changes: 13 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,19 @@ PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1,
/* }}} */

#ifdef PHP_WIN32
PHPAPI ZEND_COLD void php_win32_docref1_from_error(DWORD error, const char *param1) {
char *buf = php_win32_error_to_msg(error);
size_t buf_len;

buf_len = strlen(buf);
if (buf_len >= 2) {
buf[buf_len - 1] = '\0';
buf[buf_len - 2] = '\0';
}
php_error_docref1(NULL, param1, E_WARNING, "%s (code: %lu)", buf, error);
php_win32_error_msg_free(buf);
}

PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2) {
char *buf = php_win32_error_to_msg(error);
size_t buf_len;
Expand Down
1 change: 1 addition & 0 deletions main/php.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ PHPAPI ZEND_COLD void php_error_docref1(const char *docref, const char *param1,
PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, const char *param2, int type, const char *format, ...)
PHP_ATTRIBUTE_FORMAT(printf, 5, 6);
#ifdef PHP_WIN32
PHPAPI ZEND_COLD void php_win32_docref1_from_error(DWORD error, const char *param1);
PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2);
#endif
END_EXTERN_C()
Expand Down
2 changes: 1 addition & 1 deletion main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, const

#ifdef PHP_WIN32
if (!dir) {
php_win32_docref2_from_error(GetLastError(), path, path);
php_win32_docref1_from_error(GetLastError(), path);
}

if (dir && dir->finished) {
Expand Down