From 2df515fb4b249b5d632d3551a38bcaa4d91171ec Mon Sep 17 00:00:00 2001 From: laixintao Date: Fri, 16 Feb 2024 17:42:38 +0800 Subject: [PATCH] bugfix: split_command_args trate the \n to 2 chars in quotes. fix: https://github.com/laixintao/iredis/issues/489 --- iredis/commands.py | 2 +- iredis/utils.py | 13 +++++++------ tests/unittests/test_utils.py | 1 + 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/iredis/commands.py b/iredis/commands.py index cf21a545..3580cdbe 100644 --- a/iredis/commands.py +++ b/iredis/commands.py @@ -120,7 +120,7 @@ def split_command_args(command): # `command` with `args` is ('in') which is an invalid case. normalized_input_command = " ".join(command.split()).upper() if ( - re.search("\s", command) + re.search(r"\s", command) and command_name.startswith(normalized_input_command) and command_name != normalized_input_command ): diff --git a/iredis/utils.py b/iredis/utils.py index 5a614258..aa005338 100644 --- a/iredis/utils.py +++ b/iredis/utils.py @@ -39,10 +39,11 @@ def literal_bytes(b): return b -def _valid_token(words): - token = "".join(words).strip() - if token: - yield token +def nappend(word, c, pre_back_slash): + if pre_back_slash and c == "n": # \n + word[-1] = "\n" + else: + word.append(c) def strip_quote_args(s): @@ -69,7 +70,7 @@ def strip_quote_args(s): # previous char is \ , merge with current " word[-1] = char else: - word.append(char) + nappend(word, char, pre_back_slash) # not in quote else: # separator @@ -81,7 +82,7 @@ def strip_quote_args(s): elif char in ["'", '"']: in_quote = char else: - word.append(char) + nappend(word, char, pre_back_slash) if char == "\\" and not pre_back_slash: pre_back_slash = True else: diff --git a/tests/unittests/test_utils.py b/tests/unittests/test_utils.py index 98ea8db2..e00eafff 100644 --- a/tests/unittests/test_utils.py +++ b/tests/unittests/test_utils.py @@ -55,6 +55,7 @@ def test_timer(): (r'""', [""]), # set foo "" is a legal command (r"\\", ["\\\\"]), # backslash are legal ("\\hello\\", ["\\hello\\"]), # backslash are legal + ('foo "bar\\n1"', ["foo", "bar\n1"]), ], ) def test_stripe_quote_escape_in_quote(test_input, expected):