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

Fix Marshal.dump for String/Regexp with instance variables #2380

Merged
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
8 changes: 2 additions & 6 deletions spec/core/marshal/dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,7 @@ def _dump(level)
it "dumps a String with instance variables" do
str = +""
str.instance_variable_set("@foo", "bar")
NATFIXME 'dumps a String with instance variables', exception: SpecFailedException do
Marshal.dump(str.force_encoding("binary")).should == "\x04\bI\"\x00\x06:\t@foo\"\bbar"
end
Marshal.dump(str.force_encoding("binary")).should == "\x04\bI\"\x00\x06:\t@foo\"\bbar"
end

it "dumps a US-ASCII String" do
Expand Down Expand Up @@ -400,9 +398,7 @@ def _dump(level)
it "dumps a Regexp with instance variables" do
o = Regexp.new("")
o.instance_variable_set(:@ivar, :ivar)
NATFIXME 'dumps a Regexp with instance variables', exception: SpecFailedException do
Marshal.dump(o).should == "\x04\bI/\x00\x00\a:\x06EF:\n@ivar:\tivar"
end
Marshal.dump(o).should == "\x04\bI/\x00\x00\a:\x06EF:\n@ivar:\tivar"
end

it "dumps an extended Regexp" do
Expand Down
2 changes: 1 addition & 1 deletion spec/core/marshal/shared/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@
h = { key: s }
h.instance_variable_set :@hash_ivar, 'hash ivar'

NATFIXME 'preserves hash ivars when hash contains a string having ivar', exception: SpecFailedException do
NATFIXME 'issue with additional @ in ivar name', exception: NameError do
unmarshalled = Marshal.send(@method, Marshal.dump(h))
unmarshalled.instance_variable_get(:@hash_ivar).should == 'hash ivar'
unmarshalled[:key].instance_variable_get(:@string_ivar).should == 'string ivar'
Expand Down
18 changes: 10 additions & 8 deletions src/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ def write_string_bytes(value)
end

def write_encoding_bytes(value)
ivars = value.instance_variables.map { |ivar_name| [ivar_name, value.instance_variable_get(ivar_name)] }
case value.encoding
when Encoding::US_ASCII
write_integer_bytes(1)
write_symbol(:E)
write_false
ivars.prepend([:E, false])
when Encoding::UTF_8
write_integer_bytes(1)
write_symbol(:E)
write_true
ivars.prepend([:E, true])
end
write_integer_bytes(ivars.size) unless ivars.empty?
ivars.each do |ivar_name, ivar_value|
write(ivar_name)
write(ivar_value)
end
end

Expand Down Expand Up @@ -141,10 +143,10 @@ def write_integer(value)
end

def write_string(value)
write_char('I') if value.encoding != Encoding::ASCII_8BIT
write_char('I') if value.encoding != Encoding::ASCII_8BIT || !value.instance_variables.empty?
write_char('"')
write_string_bytes(value)
write_encoding_bytes(value) if value.encoding != Encoding::ASCII_8BIT
write_encoding_bytes(value)
end

def write_symbol(value)
Expand Down
Loading