Skip to content

Commit

Permalink
Merge branch 'PHP-8.4'
Browse files Browse the repository at this point in the history
* PHP-8.4:
  Fix GH-17067: glob:// wrapper doesn't cater to CWD for ZTS builds
  • Loading branch information
cmb69 committed Dec 18, 2024
2 parents 2e1d678 + a8ffabf commit 4dc0555
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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

0 comments on commit 4dc0555

Please sign in to comment.