Handle long commands safely in parser

This commit is contained in:
Julien Letessier 2026-01-15 12:31:53 +01:00
parent a47a3c8cc0
commit d4b10d5065
3 changed files with 37 additions and 0 deletions

View File

@ -470,6 +470,8 @@ void RequestParser::parseCmd()
if (mArgLen >= Const::MaxCmdLen) { if (mArgLen >= Const::MaxCmdLen) {
mStatus = CmdError; mStatus = CmdError;
mType = Command::None; mType = Command::None;
// Keep mCommand non-null for downstream state handling.
mCommand = &Command::get(Command::None);
logNotice("unknown request cmd too long:%s...", mCmd); logNotice("unknown request cmd too long:%s...", mCmd);
return; return;
} }

View File

@ -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")

View File

@ -143,6 +143,7 @@ TESTS=(
"test/request_parser_boundary.py" "test/request_parser_boundary.py"
"test/request_parser_error_log.py" "test/request_parser_error_log.py"
"test/response_parser_error_log.py" "test/response_parser_error_log.py"
"test/request_parser_long_command.py"
"test/pubsub_long_name.py" "test/pubsub_long_name.py"
"test/pubsub_large_message.py" "test/pubsub_large_message.py"
"test/transaction_forbid.py" "test/transaction_forbid.py"