Skip to content

Fix #78939 - Windows relative path with drive letter #5001

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
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
24 changes: 14 additions & 10 deletions Zend/zend_virtual_cwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
memcpy(resolved_path , path, path_length + 1);
} else {
size_t state_cwd_length = state->cwd_length;
memcpy(resolved_path, state->cwd, state_cwd_length);

#ifdef ZEND_WIN32
if (IS_SLASH(path[0])) {
Expand All @@ -1032,12 +1033,24 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
state_cwd_length++;
}
}
} else if (path_length >= 3 && path[1] == ':') {
/* Relative path with drive name */
if (state->cwd[1] == ':') {
if (toupper(path[0]) == toupper(state->cwd[0])) {
/* Same drive name, append path */
} else {
/* Different drive name, change cwd to the target drive root */
state_cwd_length = 2;
resolved_path[0] = path[0];
}
path += 2;
path_length -= 2;
}
}
#endif
if (path_length + state_cwd_length + 1 >= MAXPATHLEN-1) {
return 1;
}
memcpy(resolved_path, state->cwd, state_cwd_length);
if (resolved_path[state_cwd_length-1] == DEFAULT_SLASH) {
memcpy(resolved_path + state_cwd_length, path, path_length + 1);
path_length += state_cwd_length;
Expand All @@ -1048,15 +1061,6 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
}
}
} else {
#ifdef ZEND_WIN32
if (path_length > 2 && path[1] == ':' && !IS_SLASH(path[2])) {
resolved_path[0] = path[0];
resolved_path[1] = ':';
resolved_path[2] = DEFAULT_SLASH;
memcpy(resolved_path + 3, path + 2, path_length - 1);
path_length++;
} else
#endif
memcpy(resolved_path, path, path_length + 1);
}

Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_virtual_cwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ typedef unsigned short mode_t;
/* COPY_WHEN_ABSOLUTE is 2 under Win32 because by chance both regular absolute paths
in the file system and UNC paths need copying of two characters */
#define COPY_WHEN_ABSOLUTE(path) 2
#define IS_ABSOLUTE_WIN32_LOCAL_PATH(path, len) \
(len >= 3 && isalpha(path[0]) && path[1] == ':' && IS_SLASH(path[2]))
#define IS_UNC_PATH(path, len) \
(len >= 2 && IS_SLASH(path[0]) && IS_SLASH(path[1]))
#define IS_ABSOLUTE_PATH(path, len) \
(len >= 2 && (/* is local */isalpha(path[0]) && path[1] == ':' || /* is UNC */IS_SLASH(path[0]) && IS_SLASH(path[1])))
(IS_ABSOLUTE_WIN32_LOCAL_PATH(path, len) || IS_UNC_PATH(path, len))

#else
#ifdef HAVE_DIRENT_H
Expand Down
53 changes: 53 additions & 0 deletions ext/standard/tests/file/bug78939.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
Bug #78939 (Windows relative path with drive letter)
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) !== 'WIN') die('skip test is for Windows only');
?>
--FILE--
<?php
list($drive, $dirA, $dirB) = explode('\\', __FILE__, 4);
var_dump(file_exists($drive . '\\'));
var_dump(file_exists($drive . '\\' . $dirA));
var_dump(file_exists($drive . '\\' . $dirA . '\\' . $dirB));
var_dump(is_dir($drive . '\\' . $dirA . '\\' . $dirB));
echo "\n";

var_dump(chdir($drive . '\\'));
var_dump(file_exists($drive . $dirA));
var_dump(is_dir($drive . $dirA));
var_dump(file_exists($drive . $dirB));
var_dump(is_dir($drive . $dirB));
var_dump(file_exists($drive . $dirA . '/' . $dirB));
var_dump(is_dir($drive . $dirA . '/' . $dirB));
echo "\n";

var_dump(chdir($drive . '\\' . $dirA));
var_dump(file_exists($drive . $dirA));
var_dump(is_dir($drive . $dirA));
var_dump(file_exists($drive . $dirB));
var_dump(is_dir($drive . $dirB));
var_dump(file_exists($drive . $dirA . '/' . $dirB));
var_dump(is_dir($drive . $dirA . '/' . $dirB));
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)

bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)

bool(true)
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)
bool(false)
6 changes: 5 additions & 1 deletion main/streams/plain_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,11 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, const char *dir, i
}
else {
/* find a top level directory we need to create */
while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (offset != 1 && (p = strrchr(buf, DEFAULT_SLASH))) ) {
while ( (p = strrchr(buf + offset, DEFAULT_SLASH)) || (!(offset == 1
#ifdef PHP_WIN32
|| (offset == 3 && IS_ABSOLUTE_WIN32_LOCAL_PATH(buf, offset))
#endif
) && (p = strrchr(buf, DEFAULT_SLASH))) ) {
int n = 0;

*p = '\0';
Expand Down