From 9f6bce23872197b79462823121f990c487241554 Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Sun, 11 Aug 2024 06:48:30 +0200 Subject: [PATCH] Serializer: for Marshal, catch all .load errors 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. --- lib/dalli/protocol/value_serializer.rb | 15 ++++++++++++++- test/protocol/test_value_serializer.rb | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/dalli/protocol/value_serializer.rb b/lib/dalli/protocol/value_serializer.rb index f1e3cd87..b0b06ee5 100644 --- a/lib/dalli/protocol/value_serializer.rb +++ b/lib/dalli/protocol/value_serializer.rb @@ -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 diff --git a/test/protocol/test_value_serializer.rb b/test/protocol/test_value_serializer.rb index ef73ccc0..8b7d002d 100644 --- a/test/protocol/test_value_serializer.rb +++ b/test/protocol/test_value_serializer.rb @@ -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