1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/-ext-/string
usa 689a6a0a76 merge revision(s) 3f9562015e651735bfc2fdd14e8f6963b673e22a,c06ddfee878524168e4af07443217ed2f8d0954b,3b3b4a44e5: [Backport #15792]
Get rid of indirect sharing

	* string.c (str_duplicate): share the root shared string if the
	  original string is already sharing, so that all shared strings
	  refer the root shared string directly.  indirect sharing can
	  cause a dangling pointer.

	[Bug #15792]

	str_duplicate: Don't share with a frozen shared string

	This is a follow up for 3f9562015e.
	Before this commit, it was possible to create a shared string which
	shares with another shared string by passing a frozen shared string
	to `str_duplicate`.

	Such string looks like:

	```
	 --------                    -----------------
	 | root | ------ owns -----> | root's buffer |
	 --------                    -----------------
	     ^                             ^   ^
	 -----------                       |   |
	 | shared1 | ------ references -----   |
	 -----------                           |
	     ^                                 |
	 -----------                           |
	 | shared2 | ------ references ---------
	 -----------
	```

	This is bad news because `rb_fstring(shared2)` can make `shared1`
	independent, which severs the reference from `shared1` to `root`:

	```c
	/* from fstr_update_callback() */
	str = str_new_frozen(rb_cString, shared2);  /* can return shared1 */
	if (STR_SHARED_P(str)) { /* shared1 is also a shared string */
	    str_make_independent(str);  /* no frozen check */
	}
	```

	If `shared1` was the only reference to `root`, then `root` can be
	reclaimed by the GC, leaving `shared2` in a corrupted state:

	```
	 -----------                         --------------------
	 | shared1 | -------- owns --------> | shared1's buffer |
	 -----------                         --------------------
	      ^
	      |
	 -----------                         -------------------------
	 | shared2 | ------ references ----> | root's buffer (freed) |
	 -----------                         -------------------------
	```

	Here is a reproduction script for the situation this commit fixes.

	```ruby
	a = ('a' * 24).strip.freeze.strip
	-a
	p a
	4.times { GC.start }
	p a
	```

	 - string.c (str_duplicate): always share with the root string when
	   the original is a shared string.
	 - test_rb_str_dup.rb: specifically test `rb_str_dup` to make
	   sure it does not try to share with a shared string.

	[Bug #15792]

	Closes: https://github.com/ruby/ruby/pull/2159

	Update dependencies


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_5@67766 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-08-26 15:58:57 +00:00
..
test_capacity.rb Fixed misspelling words. 2017-10-22 11:27:06 +00:00
test_coderange.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_cstr.rb string.c: return reallocated pointer 2016-05-30 07:20:28 +00:00
test_ellipsize.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_enc_associate.rb Add FrozenError as a subclass of RuntimeError 2017-12-12 00:46:34 +00:00
test_enc_str_buf_cat.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_external_new.rb Add test for Bug::String.buf_new 2017-12-03 12:06:16 +00:00
test_fstring.rb test_fstring.rb: fix exception 2016-11-12 13:24:23 +00:00
test_modify_expand.rb test_modify_expand.rb: skip if no overflow 2016-12-22 23:20:00 +00:00
test_nofree.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_normalize.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_qsort.rb Add frozen_string_literal: false for all files 2015-12-16 05:07:31 +00:00
test_rb_str_dup.rb merge revision(s) 3f9562015e651735bfc2fdd14e8f6963b673e22a,c06ddfee878524168e4af07443217ed2f8d0954b,3b3b4a44e5: [Backport #15792] 2019-08-26 15:58:57 +00:00
test_set_len.rb string.c: fix buffer overflow check condition in rb_str_set_len() 2016-09-13 07:08:15 +00:00