Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: do not use a shell in proc_open if not really needed #13778

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion ext/curl/tests/curl_setopt_ssl.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,22 @@ if ($serverCert === false
$port = 14430;

// set up local server
$cmd = "openssl s_server -key $serverKeyPath -cert $serverCertPath -accept $port -www -CAfile $clientCertPath -verify_return_error -Verify 1";
$cmd = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also playing arround with this kind of array-type arg passing to proc_open.

I wonder how its possible to make stderr to stdout redirection work in this situation. usually I would use '2>&1' but it seems in the array-type notation, this arg gets escaped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command is executed as is by the operating system, there is no shell , ergo no redirection.
proc_open implements the redirect option in descriptorspec (which is unfortunately not documented) see the general_functions/proc_open_redirect.phpt test for examples on how to do the 2>&1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for taking the time to answer my silly questions. really appreciate it.

'openssl',
's_server',
'-key',
$serverKeyPath,
'-cert',
$serverCertPath,
'-accept',
$port,
'-www',
'-CAfile',
$clientCertPath,
'-verify_return_error',
'-Verify',
1
];
$process = proc_open($cmd, [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes);

if ($process === false) {
Expand Down
7 changes: 6 additions & 1 deletion ext/openssl/tests/stream_server_reneg_limit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ $serverCode = sprintf($serverCode, $certFile);
$clientCode = <<<'CODE'
phpt_wait();

$cmd = 'openssl s_client -connect 127.0.0.1:64321';
$cmd = [
'openssl',
's_client',
'-connect',
'127.0.0.1:64321'
];
$descriptorSpec = [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]];
$process = proc_open($cmd, $descriptorSpec, $pipes);

Expand Down
2 changes: 1 addition & 1 deletion ext/pcntl/tests/waiting_on_sigchild_pcntl_wait.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pcntl_signal(SIGCHLD, function($sig, $info) use (&$processes) {

for ($i = 0; $i <= 5; $i++) {
// Sleeping ensures we get to add the process to the list before the signal is invoked.
$process = proc_open('sleep 1', [], $pipes);
$process = proc_open(['sleep', '1'], [], $pipes);
$pid = proc_get_status($process)['pid'];
$processes[$pid] = $process;
}
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/tests/file/proc_open01.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ proc_open() regression test 1 (proc_open() leak)
<?php
$pipes = array(1, 2, 3);
$orig_pipes = $pipes;
$php = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
$php = getenv('TEST_PHP_EXECUTABLE');
if ($php === false) {
die("no php executable defined");
}
$proc = proc_open(
"$php -n",
[$php, '-n'],
array(0 => array('pipe', 'r'), 1 => array('pipe', 'w')),
$pipes, getcwd(), array(), array()
);
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/tests/general_functions/bug34794.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ if (!is_executable('/bin/cat')) echo 'skip cat not found';
--FILE--
<?php
echo "Opening process 1\n";
$process1 = proc_open('/bin/cat', array(0 => array('pipe', 'r'), 1 =>array('pipe', 'r')), $pipes1);
$process1 = proc_open(['/bin/cat'], array(0 => array('pipe', 'r'), 1 =>array('pipe', 'r')), $pipes1);

echo "Opening process 2\n";
$process2 = proc_open('/bin/cat', array(0 => array('pipe', 'r'), 1 =>array('pipe', 'r')), $pipes2);
$process2 = proc_open(['/bin/cat'], array(0 => array('pipe', 'r'), 1 =>array('pipe', 'r')), $pipes2);


echo "Closing process 1\n";
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/general_functions/bug44667.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $descriptor_spec = array(
1 => array('pipe', 'wb'),
);

$proc = proc_open('cat', $descriptor_spec, $pipes);
$proc = proc_open(['cat'], $descriptor_spec, $pipes);

fwrite($pipes[0], 'Hello', 5);
fflush($pipes[0]);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/general_functions/gh10239_1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (getenv("SKIP_SLOW_TESTS")) die('skip slow test');
?>
--FILE--
<?php
$p = proc_open('sleep 1', array(), $foo);
$p = proc_open(['sleep', '1'], array(), $foo);
do {
usleep(100000);
$s = proc_get_status($p);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/general_functions/gh10239_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (getenv("SKIP_SLOW_TESTS")) die('skip slow test');
?>
--FILE--
<?php
$p = proc_open('false', array(), $foo);
$p = proc_open(['false'], array(), $foo);
usleep(2 * 1000 * 1000);
var_dump(proc_get_status($p));
var_dump(proc_get_status($p));
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/general_functions/gh12655.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ foreach ( $descriptor_spec as $fd => &$d )
// don't do anything, just the fact that we used "&$d" will sink the ship!
}

$proc = proc_open(PHP_BINARY, $descriptor_spec, $pipes);
$proc = proc_open([PHP_BINARY], $descriptor_spec, $pipes);
echo $proc === false ? "FAILED\n" : "SUCCEEDED\n";

?>
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/general_functions/proc_open.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $ds = array(
);

$cat = proc_open(
"/bin/cat",
['/bin/cat'],
$ds,
$pipes
);
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/tests/general_functions/proc_open_pipes1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ for ($i = 3; $i<= 30; $i++) {
$spec[$i] = array('pipe', 'w');
}

$php = getenv("TEST_PHP_EXECUTABLE_ESCAPED");
$callee = escapeshellarg(__DIR__ . "/proc_open_pipes_sleep.inc");
proc_open("$php -n $callee", $spec, $pipes);
$php = getenv("TEST_PHP_EXECUTABLE");
$callee = __DIR__ . "/proc_open_pipes_sleep.inc";
proc_open([$php, '-n', $callee], $spec, $pipes);

var_dump(count($spec));
var_dump($pipes);
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/tests/general_functions/proc_open_pipes2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ proc_open() with no pipes

$spec = array();

$php = getenv("TEST_PHP_EXECUTABLE_ESCAPED");
$callee = escapeshellarg(__DIR__ . "/proc_open_pipes_sleep.inc");
proc_open("$php -n $callee", $spec, $pipes);
$php = getenv("TEST_PHP_EXECUTABLE");
$callee = __DIR__ . "/proc_open_pipes_sleep.inc";
proc_open([$php, "-n", $callee], $spec, $pipes);

var_dump(count($spec));
var_dump($pipes);
Expand Down
11 changes: 5 additions & 6 deletions ext/standard/tests/general_functions/proc_open_pipes3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ for ($i = 3; $i<= 5; $i++) {
$spec[$i] = array('pipe', 'w');
}

$php = getenv("TEST_PHP_EXECUTABLE_ESCAPED");
$php = getenv("TEST_PHP_EXECUTABLE");
$callee = __DIR__ . "/proc_open_pipes_sleep.inc";
$callee_escaped = escapeshellarg($callee);

$spec[$i] = array('pi');
proc_open("$php -n $callee_escaped", $spec, $pipes);
proc_open([$php, "-n", $callee], $spec, $pipes);

$spec[$i] = 1;
try {
proc_open("$php -n $callee_escaped", $spec, $pipes);
proc_open([$php, "-n", $callee], $spec, $pipes);
} catch (ValueError $exception) {
echo $exception->getMessage() . "\n";
}

$spec[$i] = array('pipe', "test");
proc_open("$php -n $callee_escaped", $spec, $pipes);
proc_open([$php, "-n", $callee], $spec, $pipes);
var_dump($pipes);

$spec[$i] = array('file', "test", "z");
proc_open("$php -n $callee_escaped", $spec, $pipes);
proc_open([$php, "-n", $callee], $spec, $pipes);
var_dump($pipes);

echo "END\n";
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/tests/streams/bug46024.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Bug #46024 stream_select() doesn't return the correct number
--SKIPIF--
<?php
if (!getenv('TEST_PHP_EXECUTABLE_ESCAPED')) die("skip TEST_PHP_EXECUTABLE_ESCAPED not defined");
if (!getenv('TEST_PHP_EXECUTABLE')) die("skip TEST_PHP_EXECUTABLE not defined");
// Terminating the process may cause a bailout while writing out the phpinfo,
// which may leak a temporary hash table. This does not seems worth fixing.
if (getenv('SKIP_ASAN')) die("xleak Test may leak");
Expand All @@ -11,9 +11,9 @@ if (getenv('SKIP_ASAN')) die("xleak Test may leak");
<?php
$pipes = array();
$proc = proc_open(
getenv('TEST_PHP_EXECUTABLE_ESCAPED') . " -n -i"
,array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'))
,$pipes, __DIR__, array(), array()
[getenv('TEST_PHP_EXECUTABLE'), '-n', '-i'],
array(0 => array('pipe', 'r'), 1 => array('pipe', 'w')),
$pipes, __DIR__, array(), array()
);
var_dump($proc);
if (!$proc) {
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/streams/bug60602.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $descs = array(

$environment = array('test' => array(1, 2, 3));

$cmd = (substr(PHP_OS, 0, 3) == 'WIN') ? 'dir' : 'ls';
$cmd = [(substr(PHP_OS, 0, 3) == 'WIN') ? 'dir' : 'ls'];
$p = proc_open($cmd, $descs, $pipes, '.', $environment);

if (is_resource($p)) {
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/streams/bug70198.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (!\$socket) {
SRV;
file_put_contents($srv_fl, $srv_fl_cont);
$dummy0 = $dummy1 = array();
$srv_proc = proc_open(getenv('TEST_PHP_EXECUTABLE_ESCAPED') . " -n $srv_fl_escaped", $dummy0, $dummy1);
$srv_proc = proc_open([getenv('TEST_PHP_EXECUTABLE'), "-n", $srv_fl], $dummy0, $dummy1);

$i = 0;
/* wait a bit for the server startup */
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/streams/bug72853.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $descs = array(
1 => array('pipe', 'w'), // stdout
);

$p = proc_open("ls", $descs, $pipes, '.', NULL, NULL);
$p = proc_open(['ls'], $descs, $pipes, '.', NULL, NULL);

stream_set_blocking($pipes[1], false);
var_dump(stream_get_meta_data($pipes[1]));
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/streams/proc_open_bug60120.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (\$input) {
TMPFILE
);

$command = sprintf("%s -n %s", getenv('TEST_PHP_EXECUTABLE_ESCAPED'), escapeshellarg($file));
$command = [getenv('TEST_PHP_EXECUTABLE'), '-n', $file];

$process = proc_open(
$command,
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/streams/proc_open_bug69900.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ file_put_contents($fl, $test_content);
$descriptorspec = array(0 => array("pipe", "r"),1 => array("pipe", "w"));
$pipes = array();

$process = proc_open(getenv('TEST_PHP_EXECUTABLE_ESCAPED').' -n -f ' . escapeshellarg($fl), $descriptorspec, $pipes, NULL, NULL, array("blocking_pipes" => true));
$process = proc_open([getenv('TEST_PHP_EXECUTABLE'), '-n', '-f', $fl], $descriptorspec, $pipes, NULL, NULL, array("blocking_pipes" => true));

$moreThanLimit = 0;
for($i = 0; $i < 10; $i++){
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/streams/stream_cast_loses_data.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fgets($stream);

// cast $stream and read fd until eof. Print each line that was read, prefixed with "proc open stdin:"
$process = proc_open(
'sed "s/^/proc open stdin:/"',
['sed', 's/^/proc open stdin:/'],
[
0 => $stream,
1 => ['pipe', 'w'],
Expand Down
4 changes: 2 additions & 2 deletions sapi/cli/tests/022.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (substr(PHP_OS, 0, 3) == "WIN") die("skip non windows test");
?>
--FILE--
<?php
$php = getenv("TEST_PHP_EXECUTABLE_ESCAPED");
$php = getenv("TEST_PHP_EXECUTABLE");
$socket_file = tempnam(sys_get_temp_dir(), pathinfo(__FILE__, PATHINFO_FILENAME) . '.sock');
$test_file = __DIR__ . '/' . pathinfo(__FILE__, PATHINFO_FILENAME) . '.inc';
if (file_exists($socket_file)) {
Expand All @@ -24,7 +24,7 @@ $desc = array(
2 => STDERR,
);
$pipes = array();
$proc = proc_open("$php -n " . escapeshellarg($test_file), $desc, $pipes);
$proc = proc_open([$php, '-n', $test_file], $desc, $pipes);
var_dump($proc);
if (!$proc) {
exit(1);
Expand Down
5 changes: 2 additions & 3 deletions sapi/cli/tests/023.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ if (substr(PHP_OS, 0, 3) == "WIN") die("skip non windows test");
?>
--FILE--
<?php
$php = getenv("TEST_PHP_EXECUTABLE_ESCAPED");
$php = getenv("TEST_PHP_EXECUTABLE");
$cwd = getcwd();
$ini_file = __DIR__ . "/023.ini";
$ini_file_escaped = escapeshellarg($ini_file);
file_put_contents($ini_file, <<<INI
; no sections should match as cli doesn't support any
memory_limit = 40M
Expand All @@ -28,7 +27,7 @@ $desc = array(
2 => array("pipe", "w"),
);
$pipes = array();
$proc = proc_open("$php -c $ini_file_escaped -r 'echo ini_get(\"memory_limit\");'", $desc, $pipes);
$proc = proc_open([$php, '-c', $ini_file, '-r', 'echo ini_get("memory_limit");'], $desc, $pipes);
if (!$proc) {
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion sapi/cli/tests/sapi_windows_set_ctrl_handler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if ($is_child) {

while(1) usleep(100);
} else {
$cmd = getenv('TEST_PHP_EXECUTABLE_ESCAPED') . " -n " . $argv[0] . " 1";
$cmd = [getenv('TEST_PHP_EXECUTABLE'), "-n", $argv[0] , "1"];
$spec = [0 => ["pipe", "r"], 1 => ["pipe", "w"]];

$proc = proc_open($cmd, $spec, $pipes, NULL, NULL, ["bypass_shell" => true, "create_process_group" => true]);
Expand Down
Loading