diff --git a/lib/remcached/packet.rb b/lib/remcached/packet.rb index a09dfd8..51bc365 100644 --- a/lib/remcached/packet.rb +++ b/lib/remcached/packet.rb @@ -80,7 +80,11 @@ def self.parse_header(buf) # # return:: [String] remaining bytes def parse_body(buf) - buf, rest = buf[0..(self[:total_body_length] - 1)], buf[self[:total_body_length]..-1] + if self[:total_body_length] < 1 + buf, rest = "", buf + else + buf, rest = buf[0..(self[:total_body_length] - 1)], buf[self[:total_body_length]..-1] + end if self[:extras_length] > 0 self[:extras] = parse_extras(buf[0..(self[:extras_length]-1)]) diff --git a/spec/packet_spec.rb b/spec/packet_spec.rb index 40cb4e6..52193ea 100644 --- a/spec/packet_spec.rb +++ b/spec/packet_spec.rb @@ -121,5 +121,23 @@ @pkt[:value].should == "World" end end + + describe :parse_body do + it "should return succeeding bytes" do + s = "\x81\x01\x00\x00" + + "\x00\x00\x00\x00" + + "\x00\x00\x00\x00" + + "\x00\x00\x00\x00" + + "\x00\x00\x00\x00" + + "\x00\x00\x00\x01" + + "chunky bacon" + @pkt = Memcached::Response.parse_header(s[0..23]) + s = @pkt.parse_body(s[24..-1]) + @pkt[:status].should == 0 + @pkt[:total_body_length].should == 0 + @pkt[:value].should == "" + s.should == "chunky bacon" + end + end end end