Skip to content

Commit 0f63bee

Browse files
committedFeb 15, 2025··
Fix GH-17797: zend_test_compile_string crash on invalid script path.
When looking for the last slash of the script path, it leads to underflow being promoted to SIZE_MAX being way beyond MAXPATHLEN. close GH-17801
1 parent e1f7209 commit 0f63bee

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
 

‎NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ PHP NEWS
1313
. Fix fallback paths in fast_long_{add,sub}_function. (nielsdos)
1414
. Fixed bug GH-17718 (Calling static methods on an interface that has
1515
`__callStatic` is allowed). (timwolla)
16+
. Fixed bug GH-17797 (zend_test_compile_string crash on invalid
17+
script path). (David Carlier)
1618

1719
- FPM:
1820
. Fixed bug GH-17643 (FPM with httpd ProxyPass encoded PATH_INFO env).

‎ext/zend_test/tests/gh17797.phpt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-17797 (zend_test_compile_string crash on invalid script path)
3+
--EXTENSIONS--
4+
zend_test
5+
--CREDITS--
6+
YuanchengJiang
7+
--FILE--
8+
<?php
9+
$source = '<?php
10+
require("sumfile.php");
11+
?>';
12+
try {zend_test_compile_string($source,$source,$c);} catch (Exception $e) { echo($e); }
13+
--EXPECTF--
14+
15+
Warning: Undefined variable $c in %s on line %d
16+
17+
Deprecated: zend_test_compile_string(): Passing null to parameter #3 ($position) of type int is deprecated in %s on line %d
18+
19+
Warning: require(sumfile.php): Failed to open stream: No such file or directory in <?php
20+
require("sumfile.php");
21+
?> on line %d
22+
23+
Fatal error: Uncaught Error: Failed opening required 'sumfile.php' (include_path='.%s') in <?php
24+
require("sumfile.php");
25+
?>:%d
26+
Stack trace:
27+
#0 %s(%d): zend_test_compile_string('<?php\nrequire("...', '<?php\nrequire("...', NULL)
28+
#1 {main}
29+
thrown in <?php
30+
require("sumfile.php");
31+
?> on line %d

‎main/fopen_wrappers.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,13 @@ PHPAPI zend_string *php_resolve_path(const char *filename, size_t filename_lengt
603603
const char *exec_fname = ZSTR_VAL(exec_filename);
604604
size_t exec_fname_length = ZSTR_LEN(exec_filename);
605605

606-
while ((--exec_fname_length < SIZE_MAX) && !IS_SLASH(exec_fname[exec_fname_length]));
606+
while (exec_fname_length > 0) {
607+
--exec_fname_length;
608+
if (IS_SLASH(exec_fname[exec_fname_length])) {
609+
break;
610+
}
611+
}
612+
607613
if (exec_fname_length > 0 &&
608614
filename_length < (MAXPATHLEN - 2) &&
609615
exec_fname_length + 1 + filename_length + 1 < MAXPATHLEN) {

0 commit comments

Comments
 (0)
Please sign in to comment.