Skip to content

Commit

Permalink
Fix server crash when receiving the empty inline command (#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk authored Nov 24, 2023
1 parent bcf1c9a commit db4669a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/server/redis_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
while (!to_process_cmds->empty()) {
auto cmd_tokens = to_process_cmds->front();
to_process_cmds->pop_front();
if (cmd_tokens.empty()) continue;

bool is_multi_exec = IsFlagEnabled(Connection::kMultiExec);
if (IsFlagEnabled(redis::Connection::kCloseAfterReply) && !is_multi_exec) break;
Expand Down
1 change: 1 addition & 0 deletions src/server/redis_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Status Request::Tokenize(evbuffer *input) {
}

tokens_ = util::Split(std::string(line.get(), line.length), " \t");
if (tokens_.empty()) continue;
commands_.emplace_back(std::move(tokens_));
state_ = ArrayLen;
}
Expand Down
12 changes: 11 additions & 1 deletion tests/gocase/unit/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@ func TestProtocolNetwork(t *testing.T) {
srv := util.StartServer(t, map[string]string{})
defer srv.Close()

t.Run("handle an empty array", func(t *testing.T) {
t.Run("empty bulk array command", func(t *testing.T) {
c := srv.NewTCPClient()
defer func() { require.NoError(t, c.Close()) }()
require.NoError(t, c.Write("*-1\r\n"))
require.NoError(t, c.Write("*0\r\n"))
require.NoError(t, c.Write("\r\n"))
require.NoError(t, c.Write("*1\r\n$4\r\nPING\r\n"))
c.MustRead(t, "+PONG")
})

t.Run("empty inline command", func(t *testing.T) {
c := srv.NewTCPClient()
defer func() { require.NoError(t, c.Close()) }()
require.NoError(t, c.Write(" \r\n"))
require.NoError(t, c.Write("*1\r\n$4\r\nPING\r\n"))
c.MustRead(t, "+PONG")
})

t.Run("out of range multibulk length", func(t *testing.T) {
c := srv.NewTCPClient()
defer func() { require.NoError(t, c.Close()) }()
Expand Down

0 comments on commit db4669a

Please sign in to comment.