Permalink
Browse files

Fix multibulk responses containing nil elements

Now we can hmget/mget/multibulk get anything containing nil
elements without hanging the client.

It's not pretty, but it works.
  • Loading branch information...
1 parent cb884e2 commit 5272a13de17ec910dbce02eaa025cfcee924d658 @mattsta mattsta committed with May 30, 2010
Showing with 18 additions and 7 deletions.
  1. +14 −3 src/erldis_client.erl
  2. +4 −4 src/erldis_proto.erl
View
@@ -355,7 +355,7 @@ recv_value(Socket, NBytes) ->
case gen_tcp:recv(Socket, NBytes+2) of
{ok, Packet} ->
- %error_logger:error_report({line, Packet}),
+% error_logger:error_report({line, Packet, NBytes}),
inet:setopts(Socket, [{packet, line}]), % go back to line mode
trim2(Packet);
{error, Reason} ->
@@ -418,9 +418,20 @@ parse_state(State, Socket, Data) ->
{0, {hold, Remaining}} ->
% begin accumulation of multi bulk reply
State#redis{remaining=Remaining, pstate=read};
- {_, {read, nil}} ->
+ {N, {read, nil}} ->
% reply with nil
- send_reply(State#redis{buffer=[nil]});
+ case State#redis.pstate of
+ empty -> send_reply(State#redis{buffer=[nil]});
+ read -> NewBuffer = [nil | State#redis.buffer],
+ NewState =
+ State#redis{remaining=N,
+ buffer=NewBuffer,
+ pstate=read},
+ case N of
+ 0 -> send_reply(NewState);
+ _ -> NewState
+ end
+ end;
{_, {read, 0}} ->
% reply with nil
send_reply(State#redis{buffer=[]});
@@ -34,9 +34,9 @@ parse(empty, <<"+QUEUED">>) ->
parse(empty, <<"+PONG">>) ->
pong;
parse(empty, <<"-", Message/binary>>) ->
- {error, Message};
-parse(empty, <<"$-1">>) ->
- {read, nil};
+ {error, Message};
+parse(_, <<"$-1">>) ->
+ {read, nil};
parse(empty, <<"*-1">>) ->
{hold, nil};
parse(empty, <<"*0">>) ->
@@ -83,4 +83,4 @@ parse_stat("total_connections_received:"++Val) ->
parse_stat("total_commands_processed:"++Val) ->
{commands, list_to_integer(Val)};
parse_stat(_) ->
- undefined.
+ undefined.

0 comments on commit 5272a13

Please sign in to comment.