Skip to content

Commit a8ffabf

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix GH-17067: glob:// wrapper doesn't cater to CWD for ZTS builds
2 parents c5469fa + 53b69ba commit a8ffabf

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

Diff for: NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ PHP NEWS
8484
to incorrect error handling). (nielsdos)
8585
. Fixed bug GH-16810 (overflow on fopen HTTP wrapper timeout value).
8686
(David Carlier)
87+
. Fixed bug GH-17067 (glob:// wrapper doesn't cater to CWD for ZTS builds).
88+
(cmb)
8789

8890
- Windows:
8991
. Hardened proc_open() against cmd.exe hijacking. (cmb)

Diff for: ext/standard/tests/streams/gh17067.phpt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-17067 (glob:// wrapper doesn't cater to CWD for ZTS builds)
3+
--FILE--
4+
<?php
5+
$dir = __DIR__ . "/gh17067";
6+
mkdir($dir);
7+
touch("$dir/foo");
8+
9+
chdir($dir);
10+
var_dump(scandir("glob://*"));
11+
?>
12+
--CLEAN--
13+
<?php
14+
$dir = __DIR__ . "/gh17067";
15+
@unlink("$dir/foo");
16+
@rmdir($dir);
17+
?>
18+
--EXPECT--
19+
array(1) {
20+
[0]=>
21+
string(3) "foo"
22+
}

Diff for: main/streams/glob_wrapper.c

+38-1
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,32 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha
225225
*opened_path = zend_string_init(path, strlen(path), 0);
226226
}
227227
}
228+
const char *pattern = path;
229+
#ifdef ZTS
230+
char cwd[MAXPATHLEN];
231+
char work_pattern[MAXPATHLEN];
232+
char *result;
233+
size_t cwd_skip = 0;
234+
if (!IS_ABSOLUTE_PATH(path, strlen(path))) {
235+
result = VCWD_GETCWD(cwd, MAXPATHLEN);
236+
if (!result) {
237+
cwd[0] = '\0';
238+
}
239+
# ifdef PHP_WIN32
240+
if (IS_SLASH(*path)) {
241+
cwd[2] = '\0';
242+
}
243+
# endif
244+
cwd_skip = strlen(cwd)+1;
245+
246+
snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, path);
247+
pattern = work_pattern;
248+
}
249+
#endif
228250

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

231-
if (0 != (ret = glob(path, pglob->flags & GLOB_FLAGMASK, NULL, &pglob->glob))) {
253+
if (0 != (ret = glob(pattern, pglob->flags & GLOB_FLAGMASK, NULL, &pglob->glob))) {
232254
#ifdef GLOB_NOMATCH
233255
if (GLOB_NOMATCH != ret)
234256
#endif
@@ -238,6 +260,21 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha
238260
}
239261
}
240262

263+
#ifdef ZTS
264+
if (cwd_skip > 0) {
265+
/* strip prepended CWD */
266+
for (i = 0; i < pglob->glob.gl_pathc; i++) {
267+
char *p = pglob->glob.gl_pathv[i];
268+
char *q = p + cwd_skip;
269+
char *e = p + strlen(pglob->glob.gl_pathv[i]) - 1;
270+
while (q <= e) {
271+
*p++ = *q++;
272+
}
273+
*p = '\0';
274+
}
275+
}
276+
#endif
277+
241278
/* if open_basedir in use, check and filter restricted paths */
242279
if ((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) {
243280
pglob->open_basedir_used = true;

0 commit comments

Comments
 (0)