Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise TypeError for dumping certain classes with Marshal #2389

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading