Skip to content

Commit c149b4f

Browse files
committed
Fix missing syntax error message in cli-server router script
Fixes GH-13113 Closes GH-13275
1 parent 18cdfd3 commit c149b4f

File tree

7 files changed

+97
-43
lines changed

7 files changed

+97
-43
lines changed

Zend/zend.c

+30-24
Original file line numberDiff line numberDiff line change
@@ -1865,12 +1865,40 @@ ZEND_API ZEND_COLD void zend_user_exception_handler(void) /* {{{ */
18651865
zval_ptr_dtor(&orig_user_exception_handler);
18661866
} /* }}} */
18671867

1868+
ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle)
1869+
{
1870+
zend_op_array *op_array = zend_compile_file(file_handle, type);
1871+
if (file_handle->opened_path) {
1872+
zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1873+
}
1874+
1875+
zend_result ret = SUCCESS;
1876+
if (op_array) {
1877+
zend_execute(op_array, retval);
1878+
zend_exception_restore();
1879+
if (UNEXPECTED(EG(exception))) {
1880+
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1881+
zend_user_exception_handler();
1882+
}
1883+
if (EG(exception)) {
1884+
ret = zend_exception_error(EG(exception), E_ERROR);
1885+
}
1886+
}
1887+
zend_destroy_static_vars(op_array);
1888+
destroy_op_array(op_array);
1889+
efree_size(op_array, sizeof(zend_op_array));
1890+
} else if (type == ZEND_REQUIRE) {
1891+
ret = FAILURE;
1892+
}
1893+
1894+
return ret;
1895+
}
1896+
18681897
ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
18691898
{
18701899
va_list files;
18711900
int i;
18721901
zend_file_handle *file_handle;
1873-
zend_op_array *op_array;
18741902
zend_result ret = SUCCESS;
18751903

18761904
va_start(files, file_count);
@@ -1879,32 +1907,10 @@ ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count
18791907
if (!file_handle) {
18801908
continue;
18811909
}
1882-
18831910
if (ret == FAILURE) {
18841911
continue;
18851912
}
1886-
1887-
op_array = zend_compile_file(file_handle, type);
1888-
if (file_handle->opened_path) {
1889-
zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
1890-
}
1891-
if (op_array) {
1892-
zend_execute(op_array, retval);
1893-
zend_exception_restore();
1894-
if (UNEXPECTED(EG(exception))) {
1895-
if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
1896-
zend_user_exception_handler();
1897-
}
1898-
if (EG(exception)) {
1899-
ret = zend_exception_error(EG(exception), E_ERROR);
1900-
}
1901-
}
1902-
zend_destroy_static_vars(op_array);
1903-
destroy_op_array(op_array);
1904-
efree_size(op_array, sizeof(zend_op_array));
1905-
} else if (type==ZEND_REQUIRE) {
1906-
ret = FAILURE;
1907-
}
1913+
ret = zend_execute_script(type, retval, file_handle);
19081914
}
19091915
va_end(files);
19101916

Zend/zend_compile.h

+1
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ ZEND_API zend_op_array *compile_filename(int type, zend_string *filename);
875875
ZEND_API zend_ast *zend_compile_string_to_ast(
876876
zend_string *code, struct _zend_arena **ast_arena, zend_string *filename);
877877
ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...);
878+
ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle);
878879
ZEND_API zend_result open_file_for_scanning(zend_file_handle *file_handle);
879880
ZEND_API void init_op_array(zend_op_array *op_array, uint8_t type, int initial_ops_size);
880881
ZEND_API void destroy_op_array(zend_op_array *op_array);

ext/curl/tests/server.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function curl_cli_server_start() {
7171
}
7272

7373
register_shutdown_function(
74-
function($handle) use($router) {
74+
function($handle) {
7575
proc_terminate($handle);
7676
/* Wait for server to shutdown */
7777
for ($i = 0; $i < 60; $i++) {

main/main.c

+20-5
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

+1
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

+6-13
Original file line numberDiff line numberDiff line change
@@ -2241,20 +2241,13 @@ 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+
int sg_options_back = SG(options);
2246+
/* Don't chdir to the router script because the file path may be relative. */
2247+
SG(options) |= SAPI_OPTION_NO_CHDIR;
2248+
bool result = php_execute_script_ex(&zfd, &retval);
2249+
SG(options) = sg_options_back;
2250+
if (result) {
22582251
if (Z_TYPE(retval) != IS_UNDEF) {
22592252
decline = Z_TYPE(retval) == IS_FALSE;
22602253
zval_ptr_dtor(&retval);

sapi/cli/tests/gh13113.phpt

+38
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)