Skip to content

Commit

Permalink
Fix Marshal.dump for strings with ASCII_8BIT encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Sep 29, 2024
1 parent a12e271 commit 620a905
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
12 changes: 3 additions & 9 deletions spec/core/marshal/dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,15 @@ def _dump(level)

describe "with a String" do
it "dumps a blank String" do
NATFIXME 'dumps a blank String', exception: SpecFailedException do
Marshal.dump("".dup.force_encoding("binary")).should == "\004\b\"\000"
end
Marshal.dump("".dup.force_encoding("binary")).should == "\004\b\"\000"
end

it "dumps a short String" do
NATFIXME 'dumps a short String', exception: SpecFailedException do
Marshal.dump("short".dup.force_encoding("binary")).should == "\004\b\"\012short"
end
Marshal.dump("short".dup.force_encoding("binary")).should == "\004\b\"\012short"
end

it "dumps a long String" do
NATFIXME 'dumps a long String', exception: SpecFailedException do
Marshal.dump(("big" * 100).force_encoding("binary")).should == "\004\b\"\002,\001#{"big" * 100}"
end
Marshal.dump(("big" * 100).force_encoding("binary")).should == "\004\b\"\002,\001#{"big" * 100}"
end

it "dumps a String extended with a Module" do
Expand Down
20 changes: 7 additions & 13 deletions spec/core/marshal/shared/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,7 @@
arr = ArraySubPush.new
arr[0] = '1'
arr_dump = Marshal.dump(arr)
NATFIXME 'loads subclasses of Array with overridden << and push correctly', exception: ArgumentError, message: 'marshal data too short' do
Marshal.send(@method, arr_dump).should == arr
end
Marshal.send(@method, arr_dump).should == arr
end

it "raises a TypeError with bad Marshal version" do
Expand Down Expand Up @@ -612,7 +610,7 @@
h = { key: s }
h.instance_variable_set :@hash_ivar, 'hash ivar'

NATFIXME 'preserves hash ivars when hash contains a string having ivar', exception: ArgumentError, message: 'marshal data too short' do
NATFIXME 'preserves hash ivars when hash contains a string having ivar', exception: SpecFailedException 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 Expand Up @@ -746,9 +744,7 @@
it "loads a string through StringIO stream" do
require 'stringio'
obj = "This is a string which should be unmarshalled through StringIO stream!"
NATFIXME 'loads a string through StringIO stream', exception: ArgumentError, message: 'marshal data too short' do
Marshal.send(@method, StringIO.new(Marshal.dump(obj))).should == obj
end
Marshal.send(@method, StringIO.new(Marshal.dump(obj))).should == obj
end

it "sets binmode if it is loading through StringIO stream" do
Expand Down Expand Up @@ -810,13 +806,11 @@ def io.binmode; raise "binmode"; end
end

it "raises ArgumentError when end of byte sequence reached before string characters end" do
NATFIXME 'raises ArgumentError when end of byte sequence reached before string characters end', exception: SpecFailedException do
Marshal.dump("hello").should == "\x04\b\"\nhello"
Marshal.dump("hello").should == "\x04\b\"\nhello"

-> {
Marshal.send(@method, "\x04\b\"\nhel")
}.should raise_error(ArgumentError, "marshal data too short")
end
-> {
Marshal.send(@method, "\x04\b\"\nhel")
}.should raise_error(ArgumentError, "marshal data too short")
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ def write_integer(value)
end

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

def write_symbol(value)
Expand Down

0 comments on commit 620a905

Please sign in to comment.