From d4b10d50658271a1fbabbd4bd64bf2918423df1c Mon Sep 17 00:00:00 2001 From: Julien Letessier Date: Thu, 15 Jan 2026 12:31:53 +0100 Subject: [PATCH] Handle long commands safely in parser --- src/RequestParser.cpp | 2 ++ test/request_parser_long_command.py | 34 +++++++++++++++++++++++++++++ test/run.sh | 1 + 3 files changed, 37 insertions(+) create mode 100644 test/request_parser_long_command.py diff --git a/src/RequestParser.cpp b/src/RequestParser.cpp index 46a3da5..7aa6573 100644 --- a/src/RequestParser.cpp +++ b/src/RequestParser.cpp @@ -470,6 +470,8 @@ void RequestParser::parseCmd() if (mArgLen >= Const::MaxCmdLen) { mStatus = CmdError; mType = Command::None; + // Keep mCommand non-null for downstream state handling. + mCommand = &Command::get(Command::None); logNotice("unknown request cmd too long:%s...", mCmd); return; } diff --git a/test/request_parser_long_command.py b/test/request_parser_long_command.py new file mode 100644 index 0000000..7f6fb99 --- /dev/null +++ b/test/request_parser_long_command.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# +# Send an overlong command name to ensure parser doesn't crash. +# + +import socket +from test_util import parse_args, make_client, exit_with_result + + +def run_test(host, port): + long_cmd = b"a" * 32 + payload = b"*2\r\n$32\r\n" + long_cmd + b"\r\n$1\r\nx\r\n" + try: + sock = socket.create_connection((host, port), timeout=1.0) + sock.sendall(payload) + sock.close() + except Exception as exc: + print("WARN: long command send failed:", exc) + + try: + c = make_client(host, port) + if c.ping() is not True: + print("FAIL: ping after long command") + return False + except Exception as exc: + print("FAIL: ping after long command:", exc) + return False + return True + + +if __name__ == "__main__": + args = parse_args("Request parser long command test") + success = run_test(args.host, args.port) + exit_with_result(success, "request parser long command", "request parser long command") diff --git a/test/run.sh b/test/run.sh index 4870d39..b63a727 100755 --- a/test/run.sh +++ b/test/run.sh @@ -143,6 +143,7 @@ TESTS=( "test/request_parser_boundary.py" "test/request_parser_error_log.py" "test/response_parser_error_log.py" + "test/request_parser_long_command.py" "test/pubsub_long_name.py" "test/pubsub_large_message.py" "test/transaction_forbid.py"