Skip to content

Commit 8b20e7b

Browse files
committed
Fixed bug #77484 Zend engine crashes when calling realpath in invalid working dir
1 parent 986b9b5 commit 8b20e7b

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Zend/zend_virtual_cwd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
764764
if (i == len ||
765765
(i + 1 == len && path[i] == '.')) {
766766
/* remove double slashes and '.' */
767-
len = i - 1;
767+
len = EXPECTED(i > 0) ? i - 1 : 0;
768768
is_dir = 1;
769769
continue;
770770
} else if (i + 2 == len && path[i] == '.' && path[i+1] == '.') {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Bug #77484 Zend engine crashes when calling realpath in invalid working dir
3+
--SKIPIF--
4+
<?php
5+
if (substr(PHP_OS, 0, 3) == 'WIN') {
6+
die("skip can't remove CWD on Windows");
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
var_dump(\getcwd());
13+
14+
\mkdir(__DIR__ . "/foo");
15+
\chdir(__DIR__ . "/foo");
16+
\rmdir(__DIR__ . "/foo");
17+
18+
// Outputs: / (incorrect)
19+
var_dump(\getcwd());
20+
21+
// Outputs: false (correct)
22+
var_dump(\realpath(''));
23+
24+
// Crash
25+
var_dump(\realpath('.'), \realpath('./'));
26+
27+
?>
28+
--EXPECTF--
29+
string(%d) "%s"
30+
bool(false)
31+
bool(false)
32+
string(1) "."
33+
string(1) "."

0 commit comments

Comments
 (0)