Skip to content

Commit 2ddbeae

Browse files
bors[bot]luis4a0
authored andcommitted
Merge #3135
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]>
1 parent 3fad998 commit 2ddbeae

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/utils/utils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ std::string mp::utils::escape_for_shell(const std::string& in)
263263
{
264264
if (0xa == c) // newline
265265
{
266-
*ret_insert++ = 0x5c; // backslash
267-
*ret_insert++ = 0x20; // space
266+
*ret_insert++ = 0x22; // double quotes
267+
*ret_insert++ = 0xa; // newline
268+
*ret_insert++ = 0x22; // double quotes
268269
}
269270
else
270271
{

tests/test_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,11 @@ TEST(Utils, escape_for_shell_actually_escapes)
426426
EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\\ \\\"quotes\\\""));
427427
}
428428

429-
TEST(Utils, escape_for_shell_replaces_newlines_with_spaces)
429+
TEST(Utils, escape_for_shell_quotes_newlines)
430430
{
431431
std::string s{"I've got\nnewlines"};
432432
auto res = mp::utils::escape_for_shell(s);
433-
EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\\ newlines"));
433+
EXPECT_THAT(res, ::testing::StrEq("I\\'ve\\ got\"\n\"newlines"));
434434
}
435435

436436
TEST(Utils, escape_for_shell_quotes_empty_string)

0 commit comments

Comments
 (0)