Skip to content

Commit

Permalink
Raise TypeError for dumping certain classes with Marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Dec 14, 2024
1 parent 74363aa commit 579f4de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
26 changes: 7 additions & 19 deletions spec/core/marshal/dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 579f4de

Please sign in to comment.