[ruby-core:105611] [Ruby master Bug#18141] Marshal load with proc yield objects before they are fully initialized
From:
"nagachika (Tomoyuki Chikanaga)" <noreply@...>
Date:
2021-10-09 06:37:17 UTC
List:
ruby-core #105611
Issue #18141 has been updated by nagachika (Tomoyuki Chikanaga).
Backport changed from 2.6: REQUIRED, 2.7: REQUIRED, 3.0: REQUIRED to 2.6: REQUIRED, 2.7: REQUIRED, 3.0: DONE
ruby_3_0 fe9d33beb78d5c7932a5c2ca3953045c0ae751d5 merged revision(s) 89242279e61b023a81c58065c62a82de8829d0b3,529fc204af84f825f98f83c34b004acbaa802615.
----------------------------------------
Bug #18141: Marshal load with proc yield objects before they are fully initialized
https://bugs.ruby-lang.org/issues/18141#change-94100
* Author: byroot (Jean Boussier)
* Status: Closed
* Priority: Normal
* Backport: 2.6: REQUIRED, 2.7: REQUIRED, 3.0: DONE
----------------------------------------
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", #<Encoding:ASCII-8BIT>]
[:final, "foo", #<Encoding:UTF-8>]
```
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 `<main>
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>