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 6a781c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/dalli/protocol/value_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,22 @@ def store(value, req_options, bitflags)
NAME_ERR_STR = 'uninitialized constant'
# rubocop:enable Layout/LineLength

def retrieve(value, bitflags)
def retrieve(value, bitflags) # rubocop:disable Metrics/MethodLength
serialized = (bitflags & FLAG_SERIALIZED) != 0
serialized ? serializer.load(value) : value
if serialized
if serializer.is_a?(Marshal)
begin
serializer.load(value)
rescue StandardError => 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 6a781c8

Please sign in to comment.