From 0d4f9c1fd73b422f1b4229a41eb985a17242d8ab Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Fri, 30 Jun 2017 13:03:14 -0700 Subject: [PATCH] Fix bug in counting backspaces --- awscli/compat.py | 8 +++++++- tests/unit/test_compat.py | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/awscli/compat.py b/awscli/compat.py index 8fb213d24012..19bf70acc4e4 100644 --- a/awscli/compat.py +++ b/awscli/compat.py @@ -226,11 +226,12 @@ def _windows_shell_quote(s): # they will need to be escaped. Instead we separately keep track # of how many we've seen. num_backspaces += 1 - if character == '"': + elif character == '"': if num_backspaces > 0: # The backslashes are part of a chain that lead up to a # double quote, so they need to be escaped. buff.append('\\' * (num_backspaces * 2)) + num_backspaces = 0 # The double quote also needs to be escaped. The fact that we're # seeing it at all means that it must have been escaped in the @@ -245,6 +246,11 @@ def _windows_shell_quote(s): num_backspaces = 0 buff.append(character) + # There may be some leftover backspaces if they were on the trailing + # end, so they're added back in here. + if num_backspaces > 0: + buff.append('\\' * num_backspaces) + new_s = ''.join(buff) if ' ' in new_s or '\t' in new_s: # If there are any spaces or tabs then the string needs to be double diff --git a/tests/unit/test_compat.py b/tests/unit/test_compat.py index f546bd94c32e..a6cbd645affd 100644 --- a/tests/unit/test_compat.py +++ b/tests/unit/test_compat.py @@ -53,7 +53,8 @@ def test_compat_shell_quote_windows(): windows_cases = { '': '""', '"': '\\"', - '\\': '\\\\', + '\\': '\\', + '\\\\': '\\\\', '\\"': '\\\\\\"', '\\\\"': '\\\\\\\\\\"', 'foo bar': '"foo bar"',