Skip to content

Commit

Permalink
Support ArgumentError on "user class"
Browse files Browse the repository at this point in the history
This makes Rails recognize the unmarshalling error and deals with it
normally.
  • Loading branch information
olleolleolle committed Aug 8, 2024
1 parent 1d4cbfc commit 3b565e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dalli/protocol/value_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def store(value, req_options, bitflags)
# in current systems
# rubocop:disable Layout/LineLength
TYPE_ERR_REGEXP = %r{needs to have method `_load'|exception class/object expected|instance of IO needed|incompatible marshal file format}.freeze
ARGUMENT_ERR_REGEXP = /undefined class|marshal data too short/.freeze
ARGUMENT_ERR_REGEXP = /undefined class|marshal data too short|dump format error \(user class\)/.freeze
NAME_ERR_STR = 'uninitialized constant'
# rubocop:enable Layout/LineLength

Expand Down
16 changes: 16 additions & 0 deletions test/protocol/test_value_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@
end
end

describe 'when deserialization raises an ArgumentError for a user class' do
let(:error_message) { 'dump format error (user class)' }
let(:serializer) { Marshal }
# This is a "user class", extending a built-in class String. FooString = Class.new(String)
let(:raw_value) {"\x04\bC:\x0EFooString\"\x00" }

it 'raises UnmarshalError on that user extended class' do
FooString = Class.new(Hash) # Note that the class does not match the inheritance in the serialized data.
exception = assert_raises Dalli::UnmarshalError do
vs.retrieve(raw_value, Dalli::Protocol::ValueSerializer::FLAG_SERIALIZED)
end

assert_equal exception.message, "Unable to unmarshal value: #{error_message}"
end
end

describe 'when deserialization raises an ArgumentError for an undefined class' do
let(:error_message) { 'undefined class/module NonexistentClass' }
let(:serializer) { Marshal }
Expand Down

0 comments on commit 3b565e3

Please sign in to comment.