From: "byroot (Jean Boussier)" Date: 2021-09-28T08:42:34+00:00 Subject: [ruby-core:105462] [Ruby master Bug#18141] Marshal load with proc yield objects before they are fully initialized Issue #18141 has been updated by byroot (Jean Boussier). Status changed from Closed to Open Description updated Subject changed from Marshal load with proc yield strings before they are fully initialized to Marshal load with proc yield objects before they are fully initialized I took the liberty to re-open this issue and to rewrite it to be more generic. I wonder if it wouldn't be simpler to revert the string only fix (https://github.com/ruby/ruby/pull/4797), and then to merge the more general one (https://github.com/ruby/ruby/pull/4866), this way it would be simpler to backport. Any opinions? ---------------------------------------- Bug #18141: Marshal load with proc yield objects before they are fully initialized https://bugs.ruby-lang.org/issues/18141#change-93912 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * Backport: 2.6: REQUIRED, 2.7: REQUIRED, 3.0: REQUIRED ---------------------------------------- I assume this is a bug because I can't find any spec or test for this behaviour: Consider the following script: ```ruby payload = Marshal.dump("foo") Marshal.load(payload, -> (obj) { if obj.is_a?(String) p [obj, obj.encoding] end obj }) p [:final, string, string.encoding] ``` outputs: ```ruby ["foo", #] [:final, "foo", #] ``` So `Marshal` call the proc before the string get its encoding assigned, this is because the encoding is stored alongside as a `TYPE_IVAR`. I think in such cases `Marshal` should delay calling the proc until the object is fully restored. A corollary to this behaviour is that the following code: ```ruby Marshal.load(payload, :freeze.to_proc) ``` raises with `can't modify frozen String: "foo" (FrozenError)`. The same happens with any instance variable on `Array` or `Hash` ```ruby foo = {} foo.instance_variable_set(:@bar, 42) payload = Marshal.dump(foo) object = Marshal.load(payload, ->(obj) { if obj.is_a?(Hash) p [obj, obj.instance_variable_get(:@bar)] obj.freeze end obj }) ``` ``` [{}, nil] /tmp/marshal.rb:6:in `load': can't modify frozen Hash: {} (FrozenError) from /tmp/marshal.rb:6:in `
``` -- https://bugs.ruby-lang.org/ Unsubscribe: