Skip to content

Fixed the escaping of back slashes and << in console output #23730

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

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 4 additions & 3 deletions src/Symfony/Component/Console/Formatter/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static function escapeTrailingBackslash($text)
if ('\\' === substr($text, -1)) {
$len = strlen($text);
$text = rtrim($text, '\\');
$text .= str_repeat('<<', $len - strlen($text));
Copy link
Member

@nicolas-grekas nicolas-grekas Aug 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this here:
$text = str_replace("\0", '', $text);
(or more generally: what happens/should happen when $text already has "\0" in it?)

$text = str_replace("\0", '', $text);
$text .= str_repeat("\0", $len - strlen($text));
}

return $text;
Expand Down Expand Up @@ -165,8 +166,8 @@ public function format($message)

$output .= $this->applyCurrentStyle(substr($message, $offset));

if (false !== strpos($output, '<<')) {
return strtr($output, array('\\<' => '<', '<<' => '\\'));
if (false !== strpos($output, "\0")) {
return strtr($output, array("\0" => '\\', '\\<' => '<'));
}

return str_replace('\\<', '<', $output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function testLGCharEscaping()
$formatter = new OutputFormatter(true);

$this->assertEquals('foo<bar', $formatter->format('foo\\<bar'));
$this->assertEquals('foo << bar', $formatter->format('foo << bar'));
$this->assertEquals('foo << bar \\', $formatter->format('foo << bar \\'));
$this->assertEquals("foo << \033[32mbar \\ baz\033[39m \\", $formatter->format('foo << <info>bar \\ baz</info> \\'));
$this->assertEquals('<info>some info</info>', $formatter->format('\\<info>some info\\</info>'));
$this->assertEquals('\\<info>some info\\</info>', OutputFormatter::escape('<info>some info</info>'));

Expand Down