Skip to content

Fix GH-17067: glob:// wrapper doesn't cater to CWD for ZTS builds #17074

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
22 changes: 22 additions & 0 deletions ext/standard/tests/streams/gh17067.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
GH-17067 (glob:// wrapper doesn't cater to CWD for ZTS builds)
--FILE--
<?php
$dir = __DIR__ . "/gh17067";
mkdir($dir);
touch("$dir/foo");

chdir($dir);
var_dump(scandir("glob://*"));
?>
--CLEAN--
<?php
$dir = __DIR__ . "/gh17067";
@unlink("$dir/foo");
@rmdir($dir);
?>
--EXPECT--
array(1) {
[0]=>
string(3) "foo"
}
39 changes: 38 additions & 1 deletion main/streams/glob_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,32 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha
*opened_path = zend_string_init(path, strlen(path), 0);
}
}
const char *pattern = path;
#ifdef ZTS
char cwd[MAXPATHLEN];
char work_pattern[MAXPATHLEN];
char *result;
size_t cwd_skip = 0;
if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
result = VCWD_GETCWD(cwd, MAXPATHLEN);
if (!result) {
cwd[0] = '\0';
}
# ifdef PHP_WIN32
if (IS_SLASH(*path)) {
cwd[2] = '\0';
}
# endif
cwd_skip = strlen(cwd)+1;

snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, path);
pattern = work_pattern;
}
#endif

pglob = ecalloc(1, sizeof(*pglob));

if (0 != (ret = glob(path, pglob->flags & GLOB_FLAGMASK, NULL, &pglob->glob))) {
if (0 != (ret = glob(pattern, pglob->flags & GLOB_FLAGMASK, NULL, &pglob->glob))) {
#ifdef GLOB_NOMATCH
if (GLOB_NOMATCH != ret)
#endif
Expand All @@ -238,6 +260,21 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha
}
}

#ifdef ZTS
if (cwd_skip > 0) {
/* strip prepended CWD */
for (i = 0; i < pglob->glob.gl_pathc; i++) {
char *p = pglob->glob.gl_pathv[i];
char *q = p + cwd_skip;
char *e = p + strlen(pglob->glob.gl_pathv[i]) - 1;
while (q <= e) {
*p++ = *q++;
}
*p = '\0';
}
}
#endif

/* if open_basedir in use, check and filter restricted paths */
if ((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) {
pglob->open_basedir_used = true;
Expand Down
Loading