From 579f4dedff639d663f53117a0bab265ad81329ba Mon Sep 17 00:00:00 2001 From: Herwin Date: Sat, 14 Dec 2024 15:46:28 +0100 Subject: [PATCH] Raise TypeError for dumping certain classes with Marshal --- spec/core/marshal/dump_spec.rb | 26 +++++++------------------- src/marshal.rb | 4 ++++ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/spec/core/marshal/dump_spec.rb b/spec/core/marshal/dump_spec.rb index a66320fae..d20103d45 100644 --- a/spec/core/marshal/dump_spec.rb +++ b/spec/core/marshal/dump_spec.rb @@ -968,41 +968,29 @@ def finalizer.noop(_) describe "when passed a StringIO" do it "should raise an error" do require "stringio" - NATFIXME 'raises a TypeError if marshalling a StringIO instance', exception: SpecFailedException do - -> { Marshal.dump(StringIO.new) }.should raise_error(TypeError) - end + -> { Marshal.dump(StringIO.new) }.should raise_error(TypeError) end end it "raises a TypeError if marshalling a Method instance" do - NATFIXME 'raises a TypeError if marshalling a Method instance', exception: SpecFailedException do - -> { Marshal.dump(Marshal.method(:dump)) }.should raise_error(TypeError) - end + -> { Marshal.dump(Marshal.method(:dump)) }.should raise_error(TypeError) end it "raises a TypeError if marshalling a Proc" do - NATFIXME 'raises a TypeError if marshalling a Proc', exception: SpecFailedException do - -> { Marshal.dump(proc {}) }.should raise_error(TypeError) - end + -> { Marshal.dump(proc {}) }.should raise_error(TypeError) end it "raises a TypeError if dumping a IO/File instance" do - NATFIXME 'raises a TypeError if dumping a IO/File instance', exception: SpecFailedException do - -> { Marshal.dump(STDIN) }.should raise_error(TypeError) - -> { File.open(__FILE__) { |f| Marshal.dump(f) } }.should raise_error(TypeError) - end + -> { Marshal.dump(STDIN) }.should raise_error(TypeError) + -> { File.open(__FILE__) { |f| Marshal.dump(f) } }.should raise_error(TypeError) end it "raises a TypeError if dumping a MatchData instance" do - NATFIXME 'raises a TypeError if dumping a MatchData instance', exception: SpecFailedException do - -> { Marshal.dump(/(.)/.match("foo")) }.should raise_error(TypeError) - end + -> { Marshal.dump(/(.)/.match("foo")) }.should raise_error(TypeError) end it "raises a TypeError if dumping a Mutex instance" do m = Mutex.new - NATFIXME 'raises a TypeError if dumping a Mutex instance', exception: SpecFailedException do - -> { Marshal.dump(m) }.should raise_error(TypeError) - end + -> { Marshal.dump(m) }.should raise_error(TypeError) end end diff --git a/src/marshal.rb b/src/marshal.rb index dbcb346aa..9130a5d10 100644 --- a/src/marshal.rb +++ b/src/marshal.rb @@ -342,6 +342,10 @@ def write(value) write_user_marshaled_object_with_allocate(value) elsif value.respond_to?(:_dump, true) write_user_marshaled_object_without_allocate(value) + elsif value.is_a?(Mutex) || value.is_a?(Proc) || value.is_a?(Method) || (defined?(StringIO) && value.is_a?(StringIO)) + raise TypeError, "no _dump_data is defined for class #{value.class}" + elsif value.is_a?(MatchData) || value.is_a?(IO) + raise TypeError, "can't dump #{value.class}" elsif value.is_a?(Object) write_object(value) else