Skip to content

Commit 19c41a3

Browse files
staabmsebastianbergmann
authored andcommitted
Do not use a shell in proc_open if not really needed
1 parent f8941b6 commit 19c41a3

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/Util/PHP/AbstractPhpProcess.php

+22-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use function array_keys;
1414
use function array_merge;
1515
use function assert;
16-
use function escapeshellarg;
16+
use function explode;
1717
use function file_exists;
1818
use function file_get_contents;
1919
use function ini_get_all;
@@ -156,12 +156,15 @@ public function runTestJob(string $job, Test $test, string $processResultFile):
156156

157157
/**
158158
* Returns the command based into the configurations.
159+
*
160+
* @return string[]
159161
*/
160-
public function getCommand(array $settings, ?string $file = null): string
162+
public function getCommand(array $settings, ?string $file = null): array
161163
{
162164
$runtime = new Runtime;
163165

164-
$command = $runtime->getBinary();
166+
$command = [];
167+
$command[] = $runtime->getRawBinary();
165168

166169
if ($runtime->hasPCOV()) {
167170
$settings = array_merge(
@@ -179,29 +182,29 @@ public function getCommand(array $settings, ?string $file = null): string
179182
);
180183
}
181184

182-
$command .= $this->settingsToParameters($settings);
185+
$command = array_merge($command, $this->settingsToParameters($settings));
183186

184187
if (PHP_SAPI === 'phpdbg') {
185-
$command .= ' -qrr';
188+
$command[] = '-qrr';
186189

187190
if (!$file) {
188-
$command .= 's=';
191+
$command[] = 's=';
189192
}
190193
}
191194

192195
if ($file) {
193-
$command .= ' ' . escapeshellarg($file);
196+
$command[] = '-f';
197+
$command[] = $file;
194198
}
195199

196200
if ($this->arguments) {
197201
if (!$file) {
198-
$command .= ' --';
202+
$command[] = '--';
199203
}
200-
$command .= ' ' . $this->arguments;
201-
}
202204

203-
if ($this->stderrRedirection) {
204-
$command .= ' 2>&1';
205+
foreach (explode(' ', $this->arguments) as $arg) {
206+
$command[] = trim($arg);
207+
}
205208
}
206209

207210
return $command;
@@ -212,12 +215,16 @@ public function getCommand(array $settings, ?string $file = null): string
212215
*/
213216
abstract public function runJob(string $job, array $settings = []): array;
214217

215-
protected function settingsToParameters(array $settings): string
218+
/**
219+
* @return list<string>
220+
*/
221+
protected function settingsToParameters(array $settings): array
216222
{
217-
$buffer = '';
223+
$buffer = [];
218224

219225
foreach ($settings as $setting) {
220-
$buffer .= ' -d ' . escapeshellarg($setting);
226+
$buffer[] = '-d';
227+
$buffer[] = $setting;
221228
}
222229

223230
return $buffer;

src/Util/PHP/DefaultPhpProcess.php

+10
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
use function is_array;
1717
use function is_resource;
1818
use function proc_close;
19+
use function proc_get_status;
1920
use function proc_open;
2021
use function rewind;
2122
use function stream_get_contents;
2223
use function sys_get_temp_dir;
2324
use function tempnam;
25+
use function time_nanosleep;
2426
use function unlink;
2527
use PHPUnit\Framework\Exception;
2628

@@ -95,6 +97,10 @@ protected function runProcess(string $job, array $settings): array
9597
2 => $handles[2] ?? ['pipe', 'w'],
9698
];
9799

100+
if ($this->stderrRedirection) {
101+
$pipeSpec[2] = ['redirect', 1];
102+
}
103+
98104
$process = proc_open(
99105
$this->getCommand($settings, $this->tempFile),
100106
$pipeSpec,
@@ -115,6 +121,10 @@ protected function runProcess(string $job, array $settings): array
115121

116122
fclose($pipes[0]);
117123

124+
while (proc_get_status($process)['running'] === true) {
125+
time_nanosleep(0, 100000);
126+
}
127+
118128
$stderr = $stdout = '';
119129

120130
if (isset($pipes[1])) {

src/Util/PHP/WindowsPhpProcess.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ protected function getHandles(): array
3838

3939
protected function useTemporaryFile(): bool
4040
{
41-
return true;
41+
return false;
4242
}
4343
}

0 commit comments

Comments
 (0)