Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26032ef

Browse files
maryonicolas-grekas
authored andcommitted
[Process] Fixed incorrectly escaping arguments on Windows when inheritEnvironmentVariables is set to false
1 parent e014b8a commit 26032ef

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public function start(callable $callback = null/*, array $env = array()*/)
320320
}
321321
if ('\\' === DIRECTORY_SEPARATOR && $this->enhanceWindowsCompatibility) {
322322
$this->options['bypass_shell'] = true;
323-
$commandline = $this->prepareWindowsCommandLine($commandline, $envBackup);
323+
$commandline = $this->prepareWindowsCommandLine($commandline, $envBackup, $env);
324324
} elseif (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
325325
// last exit code is output on the fourth pipe and caught to work around --enable-sigchild
326326
$descriptors[3] = array('pipe', 'w');
@@ -1627,7 +1627,7 @@ private function doSignal($signal, $throwException)
16271627
return true;
16281628
}
16291629

1630-
private function prepareWindowsCommandLine($cmd, array &$envBackup)
1630+
private function prepareWindowsCommandLine($cmd, array &$envBackup, array &$env = null)
16311631
{
16321632
$uid = uniqid('', true);
16331633
$varCount = 0;
@@ -1640,7 +1640,7 @@ private function prepareWindowsCommandLine($cmd, array &$envBackup)
16401640
[^"%!^]*+
16411641
)++
16421642
)"/x',
1643-
function ($m) use (&$envBackup, &$varCache, &$varCount, $uid) {
1643+
function ($m) use (&$envBackup, &$env, &$varCache, &$varCount, $uid) {
16441644
if (isset($varCache[$m[0]])) {
16451645
return $varCache[$m[0]];
16461646
}
@@ -1652,10 +1652,15 @@ function ($m) use (&$envBackup, &$varCache, &$varCount, $uid) {
16521652
}
16531653

16541654
$value = str_replace(array('!LF!', '"^!"', '"^%"', '"^^"', '""'), array("\n", '!', '%', '^', '"'), $value);
1655-
$value = preg_replace('/(\\\\*)"/', '$1$1\\"', $value);
1656-
1655+
$value = '"'.preg_replace('/(\\\\*)"/', '$1$1\\"', $value).'"';
16571656
$var = $uid.++$varCount;
1658-
putenv("$var=\"$value\"");
1657+
1658+
if (null === $env) {
1659+
putenv("$var=$value");
1660+
} else {
1661+
$env[$var] = $value;
1662+
}
1663+
16591664
$envBackup[$var] = false;
16601665

16611666
return $varCache[$m[0]] = '!'.$var.'!';

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,19 @@ public function testEscapeArgument($arg)
14651465
$this->assertSame($arg, $p->getOutput());
14661466
}
14671467

1468+
/**
1469+
* @dataProvider provideEscapeArgument
1470+
* @group legacy
1471+
*/
1472+
public function testEscapeArgumentWhenInheritEnvDisabled($arg)
1473+
{
1474+
$p = new Process(array(self::$phpBin, '-r', 'echo $argv[1];', $arg), null, array('BAR' => 'BAZ'));
1475+
$p->inheritEnvironmentVariables(false);
1476+
$p->run();
1477+
1478+
$this->assertSame($arg, $p->getOutput());
1479+
}
1480+
14681481
public function provideEscapeArgument()
14691482
{
14701483
yield array('a"b%c%');

0 commit comments

Comments
 (0)