ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "byroot (Jean Boussier)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:105509] [Ruby master Bug#18141] Marshal load with proc yield objects before they are fully initialized
Date: Thu, 30 Sep 2021 15:33:58 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-93957.20210930153358.7941@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18141.20210901081741.7941@ruby-lang.org

Issue #18141 has been updated by byroot (Jean Boussier).

Status changed from Open to Closed

https://github.com/ruby/ruby/pull/4866 was merged as 529fc204af84f825f98f83c34b004acbaa802615, closing.

----------------------------------------
Bug #18141: Marshal load with proc yield objects before they are fully initialized 
https://bugs.ruby-lang.org/issues/18141#change-93957

* Author: byroot (Jean Boussier)
* Status: Closed
* 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", #<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/

  parent reply	other threads:[~2021-09-30 15:34 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-01  8:17 [ruby-core:105104] [Ruby master Bug#18141] Marshal load with proc yield strings before they are fully initialized byroot (Jean Boussier)
2021-09-01 17:57 ` [ruby-core:105107] " byroot (Jean Boussier)
2021-09-02  0:40 ` [ruby-core:105113] " nobu (Nobuyoshi Nakada)
2021-09-17 14:19 ` [ruby-core:105327] " byroot (Jean Boussier)
2021-09-18  7:34 ` [ruby-core:105336] " nagachika (Tomoyuki Chikanaga)
2021-09-18 13:45 ` [ruby-core:105342] " byroot (Jean Boussier)
2021-09-28  8:42 ` [ruby-core:105462] [Ruby master Bug#18141] Marshal load with proc yield objects " byroot (Jean Boussier)
2021-09-30 15:33 ` byroot (Jean Boussier) [this message]
2021-10-09  6:37 ` [ruby-core:105611] " nagachika (Tomoyuki Chikanaga)
2021-11-24 10:36 ` [ruby-core:106261] " usa (Usaku NAKAMURA)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-93957.20210930153358.7941@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).