Skip to content

[Console] Remove exec and replace it by shell_exec #50338

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

Merged
merged 1 commit into from
May 19, 2023
Merged
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
6 changes: 2 additions & 4 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,11 @@ private function isInteractiveInput($inputStream): bool
return self::$stdinIsInteractive = @posix_isatty(fopen('php://stdin', 'r'));
}

if (!\function_exists('exec')) {
if (!\function_exists('shell_exec')) {
return self::$stdinIsInteractive = true;
}

exec('stty 2> /dev/null', $output, $status);

return self::$stdinIsInteractive = 1 !== $status;
return self::$stdinIsInteractive = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Component/Console/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ public static function hasSttyAvailable(): bool
return self::$stty;
}

// skip check if exec function is disabled
if (!\function_exists('exec')) {
// skip check if shell_exec function is disabled
if (!\function_exists('shell_exec')) {
return false;
}

exec('stty 2>&1', $output, $exitcode);

return self::$stty = 0 === $exitcode;
return self::$stty = (bool) shell_exec('stty 2> '.('\\' === \DIRECTORY_SEPARATOR ? 'NUL' : '/dev/null'));
}

private static function initDimensions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function testAskHiddenResponse()
$this->assertEquals('8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream("8AM\n")), $this->createOutputInterface(), $question));
}

public function testAskHiddenResponseTrimmed()
public function testAskHiddenResponseNotTrimmed()
{
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('This test is not supported on Windows');
Expand All @@ -442,7 +442,7 @@ public function testAskHiddenResponseTrimmed()
$question->setHidden(true);
$question->setTrimmable(false);

$this->assertEquals(' 8AM', $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream(' 8AM')), $this->createOutputInterface(), $question));
$this->assertEquals(' 8AM'.\PHP_EOL, $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream(' 8AM'.\PHP_EOL)), $this->createOutputInterface(), $question));
}

public function testAskMultilineResponseWithEOF()
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Tests/TerminalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public function testSttyOnWindows()
$this->markTestSkipped('Must be on windows');
}

$sttyString = exec('(stty -a | grep columns) 2>&1', $output, $exitcode);
if (0 !== $exitcode) {
$sttyString = shell_exec('(stty -a | grep columns) 2> NUL');
if (!$sttyString) {
$this->markTestSkipped('Must have stty support');
}

Expand Down