From 06be66d6825d3644f72e6a283d47b1bee75122c1 Mon Sep 17 00:00:00 2001 From: MATSUBARA Nobutada Date: Tue, 17 Sep 2024 03:03:56 +0900 Subject: [PATCH] Fix to read response data using size when meta (#1007) --- CHANGELOG.md | 2 ++ lib/dalli/protocol/meta/response_processor.rb | 8 ++++++-- test/integration/test_operations.rb | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf38f81d..d9e7dfed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Dalli Changelog Unreleased ========== +- Fix cannot read response data included terminator `\r\n` when use meta protocol (matsubara0507) + 3.2.8 ========== diff --git a/lib/dalli/protocol/meta/response_processor.rb b/lib/dalli/protocol/meta/response_processor.rb index be35b5d1..c552b3ea 100644 --- a/lib/dalli/protocol/meta/response_processor.rb +++ b/lib/dalli/protocol/meta/response_processor.rb @@ -32,7 +32,7 @@ def meta_get_with_value(cache_nils: false) return cache_nils ? ::Dalli::NOT_FOUND : nil if tokens.first == EN return true unless tokens.first == VA - @value_marshaller.retrieve(read_line, bitflags_from_tokens(tokens)) + @value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)) end def meta_get_with_value_and_cas @@ -42,7 +42,7 @@ def meta_get_with_value_and_cas cas = cas_from_tokens(tokens) return [nil, cas] unless tokens.first == VA - [@value_marshaller.retrieve(read_line, bitflags_from_tokens(tokens)), cas] + [@value_marshaller.retrieve(read_data(tokens[1].to_i), bitflags_from_tokens(tokens)), cas] end def meta_get_without_value @@ -205,6 +205,10 @@ def next_line_to_tokens line = read_line line&.split || [] end + + def read_data(data_size) + @io_source.read(data_size + TERMINATOR.bytesize)&.chomp!(TERMINATOR) + end end end end diff --git a/test/integration/test_operations.rb b/test/integration/test_operations.rb index f47962b5..4ea51ce5 100644 --- a/test/integration/test_operations.rb +++ b/test/integration/test_operations.rb @@ -23,6 +23,21 @@ end end + it 'return the value that include TERMINATOR on a hit' do + memcached_persistent(p) do |dc| + dc.flush + + val1 = "12345#{Dalli::Protocol::Meta::TERMINATOR}67890" + dc.set('a', val1) + val2 = dc.get('a') + + assert_equal val1, val2 + + assert op_addset_succeeds(dc.set('a', nil)) + assert_nil dc.get('a') + end + end + it 'returns nil on a miss' do memcached_persistent(p) do |dc| assert_nil dc.get('notexist')