Skip to content

Commit 839a02b

Browse files
Merge branch '7.1' into 7.2
* 7.1: [Process] Properly deal with not-found executables on Windows [Process] Fix handling empty path found in the PATH env var with ExecutableFinder
2 parents c1a3c42 + 78a4339 commit 839a02b

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/Symfony/Component/Process/ExecutableFinder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
6969
$suffixes = array_merge($suffixes, $this->suffixes);
7070
foreach ($suffixes as $suffix) {
7171
foreach ($dirs as $dir) {
72+
if ('' === $dir) {
73+
$dir = '.';
74+
}
7275
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
7376
return $file;
7477
}
@@ -79,12 +82,12 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
7982
}
8083
}
8184

82-
if (!\function_exists('exec')) {
85+
if (!\function_exists('exec') || \strlen($name) !== strcspn($name, '/'.\DIRECTORY_SEPARATOR)) {
8386
return $default;
8487
}
8588

86-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
87-
$execResult = @exec($command.' '.escapeshellarg($name));
89+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
90+
$execResult = exec(\sprintf($command, escapeshellarg($name)));
8891

8992
if (($executablePath = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) && @is_executable($executablePath)) {
9093
return $executablePath;

src/Symfony/Component/Process/PhpExecutableFinder.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@ public function find(bool $includeArgs = true): string|false
3333
{
3434
if ($php = getenv('PHP_BINARY')) {
3535
if (!is_executable($php)) {
36-
if (!\function_exists('exec')) {
36+
if (!\function_exists('exec') || \strlen($php) !== strcspn($php, '/'.\DIRECTORY_SEPARATOR)) {
3737
return false;
3838
}
3939

40-
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where' : 'command -v --';
41-
$execResult = exec($command.' '.escapeshellarg($php));
42-
if ($php = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) {
43-
if (!is_executable($php)) {
44-
return false;
45-
}
46-
} else {
40+
$command = '\\' === \DIRECTORY_SEPARATOR ? 'where %s 2> NUL' : 'command -v -- %s';
41+
$execResult = exec(\sprintf($command, escapeshellarg($php)));
42+
if (!$php = substr($execResult, 0, strpos($execResult, \PHP_EOL) ?: null)) {
43+
return false;
44+
}
45+
if (!is_executable($php)) {
4746
return false;
4847
}
4948
}

src/Symfony/Component/Process/Tests/ExecutableFinderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ public function testFindWithOpenBaseDir()
136136
}
137137
}
138138

139+
/**
140+
* @runInSeparateProcess
141+
*/
139142
public function testFindBatchExecutableOnWindows()
140143
{
141144
if (\ini_get('open_basedir')) {
@@ -163,6 +166,24 @@ public function testFindBatchExecutableOnWindows()
163166
$this->assertSamePath($target.'.BAT', $result);
164167
}
165168

169+
/**
170+
* @runInSeparateProcess
171+
*/
172+
public function testEmptyDirInPath()
173+
{
174+
putenv(sprintf('PATH=%s:', \dirname(\PHP_BINARY)));
175+
176+
touch('executable');
177+
chmod('executable', 0700);
178+
179+
$finder = new ExecutableFinder();
180+
$result = $finder->find('executable');
181+
182+
$this->assertSame('./executable', $result);
183+
184+
unlink('executable');
185+
}
186+
166187
private function assertSamePath($expected, $tested)
167188
{
168189
if ('\\' === \DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)