Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6d39fd4

Browse files
committed
bug#21776 [Process] Fix ignoring of bad env var names (nicolas-grekas)
This PR was merged into the 3.2 branch.Discussion----------[Process] Fix ignoring of bad env var names| Q | A| ------------- | ---| Branch? | 3.2| Bug fix? | yes| New feature? | no| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#21704| License | MIT| Doc PR | -Patch backported from master, which is free from the linked issue.Commits-------406bb09 [Process] Fix ignoring of bad env var names
2 parents346eacd +406bb09 commit6d39fd4

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

‎src/Symfony/Component/Process/Process.php‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,25 @@ public function start(callable $callback = null)
266266
$this->callback =$this->buildCallback($callback);
267267
$this->hasCallback =null !==$callback;
268268
$descriptors =$this->getDescriptors();
269+
$inheritEnv =$this->inheritEnv;
269270

270271
$commandline =$this->commandline;
271-
$envline ='';
272272

273-
if (null !==$this->env &&$this->inheritEnv) {
273+
$env =$this->env;
274+
$envBackup =array();
275+
if (null !==$env &&$inheritEnv) {
274276
if ('\\' ===DIRECTORY_SEPARATOR && !empty($this->options['bypass_shell']) && !$this->enhanceWindowsCompatibility) {
275277
thrownewLogicException('The "bypass_shell" option must be false to inherit environment variables while enhanced Windows compatibility is off');
276278
}
277-
$env ='\\' ===DIRECTORY_SEPARATOR ?'(SET %s)&&' :'export %s;';
278-
foreach ($this->envas$k =>$v) {
279-
$envline .=sprintf($env, ProcessUtils::escapeArgument("$k=$v"));
279+
280+
foreach ($envas$k =>$v) {
281+
$envBackup[$k] =getenv($v);
282+
putenv(false ===$v ||null ===$v ?$k :"$k=$v");
280283
}
281284
$env =null;
282-
}else {
283-
$env =$this->env;
284285
}
285286
if ('\\' ===DIRECTORY_SEPARATOR &&$this->enhanceWindowsCompatibility) {
286-
$commandline ='cmd /V:ON /E:ON /D /C "('.$envline.$commandline.')';
287+
$commandline ='cmd /V:ON /E:ON /D /C "('.$commandline.')';
287288
foreach ($this->processPipes->getFiles()as$offset =>$filename) {
288289
$commandline .=''.$offset.'>'.ProcessUtils::escapeArgument($filename);
289290
}
@@ -297,18 +298,20 @@ public function start(callable $callback = null)
297298
$descriptors[3] =array('pipe','w');
298299

299300
// See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
300-
$commandline =$envline.'{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
301+
$commandline ='{ ('.$this->commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
301302
$commandline .='pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code';
302303

303304
// Workaround for the bug, when PTS functionality is enabled.
304305
// @see : https://bugs.php.net/69442
305306
$ptsWorkaround =fopen(__FILE__,'r');
306-
}elseif ('' !==$envline) {
307-
$commandline =$envline.$commandline;
308307
}
309308

310309
$this->process =proc_open($commandline,$descriptors,$this->processPipes->pipes,$this->cwd,$env,$this->options);
311310

311+
foreach ($envBackupas$k =>$v) {
312+
putenv(false ===$v ?$k :"$k=$v");
313+
}
314+
312315
if (!is_resource($this->process)) {
313316
thrownewRuntimeException('Unable to launch a new process.');
314317
}
@@ -1104,10 +1107,7 @@ public function setEnv(array $env)
11041107
return !is_array($value);
11051108
});
11061109

1107-
$this->env =array();
1108-
foreach ($envas$key =>$value) {
1109-
$this->env[$key] = (string)$value;
1110-
}
1110+
$this->env =$env;
11111111

11121112
return$this;
11131113
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,18 @@ public function testChainedProcesses()
13911391
$this->assertSame('456',$p2->getOutput());
13921392
}
13931393

1394+
publicfunctiontestSetBadEnv()
1395+
{
1396+
$process =$this->getProcess('echo hello');
1397+
$process->setEnv(array('bad%%' =>'123'));
1398+
$process->inheritEnvironmentVariables(true);
1399+
1400+
$process->run();
1401+
1402+
$this->assertSame('hello'.PHP_EOL,$process->getOutput());
1403+
$this->assertSame('',$process->getErrorOutput());
1404+
}
1405+
13941406
publicfunctiontestInheritEnvEnabled()
13951407
{
13961408
$process =$this->getProcess(self::$phpBin.' -r'.escapeshellarg('echo serialize($_SERVER);'),null,array('BAR' =>'BAZ'));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp