Skip to content

Commit

Permalink
Serializer: for Marshal, catch all .load errors
Browse files Browse the repository at this point in the history
In order to avoid missing an error message to filter out, treat any Marshal.load error as a
failed serialization, and trust Ruby's e.cause system to provide a
lineage of the error's true beginning.
  • Loading branch information
olleolleolle committed Aug 11, 2024
1 parent 1d4cbfc commit 9f6bce2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/dalli/protocol/value_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ def store(value, req_options, bitflags)

def retrieve(value, bitflags)
serialized = (bitflags & FLAG_SERIALIZED) != 0
serialized ? serializer.load(value) : value
if serialized
if serializer.is_a?(Marshal)
begin
serializer.load(value)
rescue => e
raise UnmarshalError, "Unable to unmarshal value: #{e.message}"
end
else
# Use Dalli's existing exception filtering for deserialization when not using Marshal to serialize.
serializer.load(value)
end
else
value
end
rescue TypeError => e
filter_type_error(e)
rescue ArgumentError => e
Expand Down
1 change: 1 addition & 0 deletions test/protocol/test_value_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@

describe 'when the bitflags specify serialization' do
it 'should deserialize the value' do
serializer.expect :is_a?, true, [Marshal]
serializer.expect :load, deserialized_dummy, [raw_value]
bitflags = rand(32)
bitflags |= 0x1
Expand Down

0 comments on commit 9f6bce2

Please sign in to comment.