Skip to content

Commit

Permalink
Fix Marshal.load with empty string argument
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Jan 25, 2023
1 parent 11fda7e commit 8d39eb3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
6 changes: 5 additions & 1 deletion spec/ruby/core/marshal/shared/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

it "raises an ArgumentError when the dumped data is truncated" do
obj = {first: 1, second: 2, third: 3}
-> { Marshal.send(@method, Marshal.dump(obj)[0, 5]) }.should raise_error(ArgumentError)
-> { Marshal.send(@method, Marshal.dump(obj)[0, 5]) }.should raise_error(ArgumentError, "marshal data too short")
end

it "raises an ArgumentError when the argument is empty String" do
-> { Marshal.send(@method, "") }.should raise_error(ArgumentError, "marshal data too short")
end

it "raises an ArgumentError when the dumped class is missing" do
Expand Down
13 changes: 3 additions & 10 deletions src/main/ruby/truffleruby/core/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,7 @@ def initialize(stream, depth, proc)
@stream = nil
end

if stream
@consumed = 2
else
@consumed = 0
end

@consumed = 0
@modules = nil
@has_ivar = []
@proc = proc
Expand Down Expand Up @@ -1327,12 +1322,10 @@ def self.dump(obj, an_io=nil, limit=nil)
def self.load(obj, prc = nil)
if Primitive.object_respond_to? obj, :to_str, false
data = obj.to_s

major = data.getbyte 0
minor = data.getbyte 1

ms = StringState.new data, nil, prc

major = ms.consume_byte
minor = ms.consume_byte
elsif Primitive.object_respond_to? obj, :read, false and
Primitive.object_respond_to? obj, :getc, false
ms = IOState.new obj, nil, prc
Expand Down

0 comments on commit 8d39eb3

Please sign in to comment.