From 763deb4c04c73baaad032bd20235077f41707072 Mon Sep 17 00:00:00 2001 From: l-pt <88390968+l-pt@users.noreply.github.com> Date: Thu, 18 Nov 2021 04:14:39 +0100 Subject: [PATCH 1/3] Add $result_code parameter to shell_exec --- ext/standard/basic_functions.stub.php | 2 +- ext/standard/basic_functions_arginfo.h | 1 + ext/standard/exec.c | 11 +++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 1719b1453795a..2cb742b091cac 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -1005,7 +1005,7 @@ function escapeshellcmd(string $command): string {} function escapeshellarg(string $arg): string {} /** @refcount 1 */ -function shell_exec(string $command): string|false|null {} +function shell_exec(string $command, int &$result_code = null): string|false|null {} #ifdef HAVE_NICE function proc_nice(int $priority): bool {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index ab7776d630919..ad178873ac9e3 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1166,6 +1166,7 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_shell_exec, 0, 1, MAY_BE_STRING|MAY_BE_FALSE|MAY_BE_NULL) ZEND_ARG_TYPE_INFO(0, command, IS_STRING, 0) + ZEND_ARG_INFO_WITH_DEFAULT_VALUE(1, result_code, "null") ZEND_END_ARG_INFO() #if defined(HAVE_NICE) diff --git a/ext/standard/exec.c b/ext/standard/exec.c index 1831b8eaa54d7..7c7e1e2be7740 100644 --- a/ext/standard/exec.c +++ b/ext/standard/exec.c @@ -515,11 +515,15 @@ PHP_FUNCTION(shell_exec) FILE *in; char *command; size_t command_len; + zval *ret_code = NULL; zend_string *ret; php_stream *stream; + int ret_pclose; - ZEND_PARSE_PARAMETERS_START(1, 1) + ZEND_PARSE_PARAMETERS_START(1, 2) Z_PARAM_STRING(command, command_len) + Z_PARAM_OPTIONAL + Z_PARAM_ZVAL(ret_code) ZEND_PARSE_PARAMETERS_END(); if (!command_len) { @@ -542,8 +546,11 @@ PHP_FUNCTION(shell_exec) stream = php_stream_fopen_from_pipe(in, "rb"); ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0); - php_stream_close(stream); + ret_pclose = php_stream_close(stream); + if (ret_code) { + ZEND_TRY_ASSIGN_REF_LONG(ret_code, ret_pclose); + } if (ret && ZSTR_LEN(ret) > 0) { RETVAL_STR(ret); } From 717a4f67b89237c1f87261e6c0d456f635cd73fd Mon Sep 17 00:00:00 2001 From: l-pt <88390968+l-pt@users.noreply.github.com> Date: Thu, 18 Nov 2021 17:59:09 +0100 Subject: [PATCH 2/3] add test case for shell_exec() with $result_code set --- .../tests/misc/shell_exec_variation.phpt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ext/standard/tests/misc/shell_exec_variation.phpt diff --git a/ext/standard/tests/misc/shell_exec_variation.phpt b/ext/standard/tests/misc/shell_exec_variation.phpt new file mode 100644 index 0000000000000..658a37d7d0869 --- /dev/null +++ b/ext/standard/tests/misc/shell_exec_variation.phpt @@ -0,0 +1,27 @@ +--TEST-- +shell_exec() with exit code setting +--SKIPIF-- + +--FILE-- + /dev/null 2>&1" +]; + +foreach ($commands as $command) { + shell_exec($command, $retcode); + var_dump($retcode); +} + +echo "Done\n"; +?> +--EXPECT-- +int(0) +int(127) +Done From eac121763a6615392546922b7ef25f548f1f96b2 Mon Sep 17 00:00:00 2001 From: l-pt <88390968+l-pt@users.noreply.github.com> Date: Thu, 18 Nov 2021 19:05:20 +0100 Subject: [PATCH 3/3] make shell_exec() stub consistent with system, passthru, exec and regenerate arginfo --- ext/standard/basic_functions.stub.php | 7 +++++-- ext/standard/basic_functions_arginfo.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 2cb742b091cac..fa7ee207eb944 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -1004,8 +1004,11 @@ function escapeshellcmd(string $command): string {} /** @refcount 1 */ function escapeshellarg(string $arg): string {} -/** @refcount 1 */ -function shell_exec(string $command, int &$result_code = null): string|false|null {} +/** + * @param int $result_code + * @refcount 1 + */ +function shell_exec(string $command, &$result_code = null): string|false|null {} #ifdef HAVE_NICE function proc_nice(int $priority): bool {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index ad178873ac9e3..5e1a8c7c76bac 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 96393b26861733e5c7fc9a12095a29c07ed73928 */ + * Stub hash: 57b39034cfe4b7421db6a6cc09ba4b086e95d646 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)