Skip to content

Commit 85c4ac8

Browse files
committed
Fix missing syntax error message in cli-server router script
Fixes GH-13113
1 parent b06311c commit 85c4ac8

File tree

6 files changed

+91
-42
lines changed

6 files changed

+91
-42
lines changed

Zend/zend.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,12 +1858,40 @@ ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
18581858
zval_ptr_dtor(&orig_user_exception_handler);
18591859
} /* }}} */
18601860

1861+
ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle)
1862+
{
1863+
zend_op_array *op_array = zend_compile_file(file_handle, type);
1864+
if (file_handle->opened_path) {
1865+
zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1866+
}
1867+
1868+
zend_result ret = SUCCESS;
1869+
if (op_array) {
1870+
zend_execute(op_array, retval);
1871+
zend_exception_restore();
1872+
if (UNEXPECTED(EG(exception))) {
1873+
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1874+
zend_user_exception_handler();
1875+
}
1876+
if (EG(exception)) {
1877+
ret = zend_exception_error(EG(exception), E_ERROR);
1878+
}
1879+
}
1880+
zend_destroy_static_vars(op_array);
1881+
destroy_op_array(op_array);
1882+
efree_size(op_array, sizeof(zend_op_array));
1883+
} else if (type == ZEND_REQUIRE) {
1884+
ret = FAILURE;
1885+
}
1886+
1887+
return ret;
1888+
}
1889+
18611890
ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
18621891
{
18631892
va_list files;
18641893
int i;
18651894
zend_file_handle *file_handle;
1866-
zend_op_array *op_array;
18671895
zend_result ret = SUCCESS;
18681896

18691897
va_start(files, file_count);
@@ -1872,32 +1900,10 @@ ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count
18721900
if (!file_handle) {
18731901
continue;
18741902
}
1875-
18761903
if (ret == FAILURE) {
18771904
continue;
18781905
}
1879-
1880-
op_array = zend_compile_file(file_handle, type);
1881-
if (file_handle->opened_path) {
1882-
zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1883-
}
1884-
if (op_array) {
1885-
zend_execute(op_array, retval);
1886-
zend_exception_restore();
1887-
if (UNEXPECTED(EG(exception))) {
1888-
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1889-
zend_user_exception_handler();
1890-
}
1891-
if (EG(exception)) {
1892-
ret = zend_exception_error(EG(exception), E_ERROR);
1893-
}
1894-
}
1895-
zend_destroy_static_vars(op_array);
1896-
destroy_op_array(op_array);
1897-
efree_size(op_array, sizeof(zend_op_array));
1898-
} else if (type==ZEND_REQUIRE) {
1899-
ret = FAILURE;
1900-
}
1906+
ret = zend_execute_script(type, retval, file_handle);
19011907
}
19021908
va_end(files);
19031909

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ ZEND_API zend_op_array *compile_filename(int type, zend_string *filename);
867867
ZEND_API zend_ast *zend_compile_string_to_ast(
868868
zend_string *code, struct _zend_arena **ast_arena, zend_string *filename);
869869
ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...);
870+
ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle);
870871
ZEND_API zend_result open_file_for_scanning(zend_file_handle *file_handle);
871872
ZEND_API void init_op_array(zend_op_array *op_array, uint8_t type, int initial_ops_size);
872873
ZEND_API void destroy_op_array(zend_op_array *op_array);

main/main.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,8 +2431,7 @@ void php_module_shutdown(void)
24312431
}
24322432
/* }}} */
24332433

2434-
/* {{{ php_execute_script */
2435-
PHPAPI bool php_execute_script(zend_file_handle *primary_file)
2434+
PHPAPI bool php_execute_script_ex(zend_file_handle *primary_file, zval *retval)
24362435
{
24372436
zend_file_handle *prepend_file_p = NULL, *append_file_p = NULL;
24382437
zend_file_handle prepend_file, append_file;
@@ -2442,7 +2441,7 @@ PHPAPI bool php_execute_script(zend_file_handle *primary_file)
24422441
char *old_cwd;
24432442
ALLOCA_FLAG(use_heap)
24442443
#endif
2445-
bool retval = false;
2444+
bool result = true;
24462445

24472446
#ifndef HAVE_BROKEN_GETCWD
24482447
# define OLD_CWD_SIZE 4096
@@ -2501,7 +2500,17 @@ PHPAPI bool php_execute_script(zend_file_handle *primary_file)
25012500
zend_set_timeout(INI_INT("max_execution_time"), 0);
25022501
}
25032502

2504-
retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
2503+
if (prepend_file_p && result) {
2504+
result = zend_execute_script(ZEND_REQUIRE, NULL, prepend_file_p) == SUCCESS;
2505+
}
2506+
if (result) {
2507+
result = zend_execute_script(ZEND_REQUIRE, retval, primary_file) == SUCCESS;
2508+
}
2509+
if (append_file_p && result) {
2510+
result = zend_execute_script(ZEND_REQUIRE, NULL, append_file_p) == SUCCESS;
2511+
}
2512+
} zend_catch {
2513+
result = false;
25052514
} zend_end_try();
25062515

25072516
if (prepend_file_p) {
@@ -2529,7 +2538,13 @@ PHPAPI bool php_execute_script(zend_file_handle *primary_file)
25292538
}
25302539
free_alloca(old_cwd, use_heap);
25312540
#endif
2532-
return retval;
2541+
return result;
2542+
}
2543+
2544+
/* {{{ php_execute_script */
2545+
PHPAPI bool php_execute_script(zend_file_handle *primary_file)
2546+
{
2547+
return php_execute_script_ex(primary_file, NULL);
25332548
}
25342549
/* }}} */
25352550

main/php_main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ PHPAPI int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals);
4545
PHPAPI zend_result php_register_extensions(zend_module_entry * const * ptr, int count);
4646

4747
PHPAPI bool php_execute_script(zend_file_handle *primary_file);
48+
PHPAPI bool php_execute_script_ex(zend_file_handle *primary_file, zval *retval);
4849
PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval *ret);
4950
PHPAPI zend_result php_lint_script(zend_file_handle *file);
5051

sapi/cli/php_cli_server.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,20 +2241,8 @@ static bool php_cli_server_dispatch_router(php_cli_server *server, php_cli_serve
22412241

22422242
zend_try {
22432243
zval retval;
2244-
2245-
/* Normally php_execute_script restarts the timer with max_execution_time if it has
2246-
* previously been initialized with max_input_time. We're not using php_execute_script here
2247-
* because it does not provide a way to get the return value of the main script, so we need
2248-
* to restart the timer manually. */
2249-
if (PG(max_input_time) != -1) {
2250-
#ifdef PHP_WIN32
2251-
zend_unset_timeout();
2252-
#endif
2253-
zend_set_timeout(INI_INT("max_execution_time"), 0);
2254-
}
2255-
22562244
ZVAL_UNDEF(&retval);
2257-
if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE, &retval, 1, &zfd)) {
2245+
if (php_execute_script_ex(&zfd, &retval)) {
22582246
if (Z_TYPE(retval) != IS_UNDEF) {
22592247
decline = Z_TYPE(retval) == IS_FALSE;
22602248
zval_ptr_dtor(&retval);

sapi/cli/tests/gh13113.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
GH-13113: Missing syntax error in CLI-server router script
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
include "php_cli_server.inc";
10+
php_cli_server_start('foo bar');
11+
12+
$host = PHP_CLI_SERVER_HOSTNAME;
13+
$fp = php_cli_server_connect();
14+
$request = <<<REQUEST
15+
GET / HTTP/1.1
16+
Host: $host
17+
18+
19+
REQUEST;
20+
21+
if(fwrite($fp, $request)) {
22+
while (!feof($fp)) {
23+
echo fgets($fp);
24+
}
25+
}
26+
27+
fclose($fp);
28+
?>
29+
--EXPECTF--
30+
HTTP/1.1 200 OK
31+
Host: %s
32+
Date: %s
33+
Connection: close
34+
X-Powered-By: PHP/%s
35+
Content-type: text/html; charset=UTF-8
36+
37+
<br />
38+
<b>Parse error</b>: syntax error, unexpected identifier &quot;bar&quot; in <b>%sindex.php</b> on line <b>1</b><br />

0 commit comments

Comments
 (0)