ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: matz@ruby-lang.org
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50576] [Ruby trunk Bug#14847] `clone` can generate strange objects
Date: Thu, 14 Jun 2018 04:07:09 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-72489.20180614040708.a613be4afa3827db@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14847.20180614011424@ruby-lang.org

Issue #14847 has been updated by matz (Yukihiro Matsumoto).


まつもと ゆきひろです

おそらくは FL_FREEZE などいくつかのフラグだけ特別にコピーするべきなんだと思います。
最近 mruby でも同様のレポートが来て、あちらでは FREEZE だけコピーすることにしました。
CRuby ではもうちょっとコピーする必要がありそうです。

----------------------------------------
Bug #14847: `clone` can generate strange objects
https://bugs.ruby-lang.org/issues/14847#change-72489

* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 2.6
* ruby -v: 2.6
* Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
`Object#clone(obj)` を呼び出すと、

* (1) `rb_obj_alloc(rb_obj_class(obj));` で空の `obj` を作り、
* (2) `RBasic(orig)->flags` を(できるだけ)引き継ぎ
* (3) 特異クラス情報を引き継ぎ
* (4) インスタンス変数を引き継ぎ
* (5) taint 情報を引き継ぎ
* (6) `initialize_copy` を呼んで初期化

という感じで処理をするのですが、(2) の箇所で `FL_USER*` がそのまま引き継がれます。

そして、`initialize_copy` が呼ばれたとき、適切に初期化をしないと、`FL_USER*` がおかしなことになります。

下記はどれも遠藤さんが見つけてくれた例です。

```
class Array; def initialize_copy(*); end; end # 何もしない
x = [1,2,3].clone; p x #=> [false, false, false]
```

EMBED_LEN だけがコピーされ、配列の中身はコピーされない(0 初期化のまま)なので false が充填された配列になる。

```
class Array; def initialize_copy(*); end; end
x = [1,2,3,4,5,6,7][1..-2].clone
x.push(1,1,1,1,1)
#=> [BUG] Segmentation fault
```

ELTS_SHARED がコピーされるが、shared root への参照がコピーされないので SEGV する

```
class Hash; def initialize_copy(*); end; end
h = {}; h.default_proc = proc { }
h = h.clone
h[1] #=> undefined method `default_proc=' for {}:Hash (NoMethodError)
```

HASH_PROC_DEFAULT (== FL_USER2) だけがコピーされ、default_proc は nil のまま。

`initialize_copy` が責務を果たしていない、というのはその通りですが、そもそも `FL_USER*` をここでコピーするのは必要なんでしょうか。
`initialize_copy` 側で、`FL_USER*` を適切に設定するべきではないでしょうか。

少なくとも、SEGV は回避しなければいけないかと思います。

歴史的経緯を知らないので、要るんだって話かもしれませんが、とりあえず現状はなんかやばそうな気がします。




-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2018-06-14  4:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14847.20180614011424@ruby-lang.org>
2018-06-14  1:14 ` [ruby-dev:50575] [Ruby trunk Bug#14847] `clone` can generate strange objects ko1
2018-06-14  4:07 ` matz [this message]

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-72489.20180614040708.a613be4afa3827db@ruby-lang.org \
    --to=ruby-dev@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
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).