Skip to content

Commit

Permalink
Merge #3135
Browse files Browse the repository at this point in the history
3135: Quote '\n' in strings which go through SSH. r=georgeliao,sharder996.townsend2010 a=luis4a0

When we need to pass a string through SSH, we escape some characters. But we forgot about newlines, which are eliminated unless quoted. This PR just put newlines in double quotes, and leave escaping the rest of the characters untouched.

Fixes #3116.

Co-authored-by: Luis Peñaranda <[email protected]>
  • Loading branch information
bors[bot] and luis4a0 authored Jun 21, 2023
2 parents 7bedc81 + 5d0d8dd commit a25fa8a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ std::string mp::utils::escape_for_shell(const std::string& in)
{
if (0xa == c) // newline
{
*ret_insert++ = 0x5c; // backslash
*ret_insert++ = 0x20; // space
*ret_insert++ = 0x22; // double quotes
*ret_insert++ = 0xa; // newline
*ret_insert++ = 0x22; // double quotes
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,11 @@ TEST(Utils, escape_for_shell_actually_escapes)
EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\\ \\\"quotes\\\""));
}

TEST(Utils, escape_for_shell_replaces_newlines_with_spaces)
TEST(Utils, escape_for_shell_quotes_newlines)
{
std::string s{"I've got\nnewlines"};
auto res = mp::utils::escape_for_shell(s);
EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\\ newlines"));
EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\"\n\"newlines"));
}

TEST(Utils, escape_for_shell_quotes_empty_string)
Expand Down

0 comments on commit a25fa8a

Please sign in to comment.