[前][次][番号順一覧][スレッド一覧]

ruby-changes:43138

From: nobu <ko1@a...>
Date: Mon, 30 May 2016 16:20:37 +0900 (JST)
Subject: [ruby-changes:43138] nobu:r55212 (trunk): string.c: return reallocated pointer

nobu	2016-05-30 16:20:28 +0900 (Mon, 30 May 2016)

  New Revision: 55212

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55212

  Log:
    string.c: return reallocated pointer
    
    * string.c (str_fill_term): return new pointer reallocated by
      filling terminator.

  Modified files:
    trunk/ChangeLog
    trunk/ext/-test-/string/cstr.c
    trunk/string.c
    trunk/test/-ext-/string/test_cstr.rb
Index: ext/-test-/string/cstr.c
===================================================================
--- ext/-test-/string/cstr.c	(revision 55211)
+++ ext/-test-/string/cstr.c	(revision 55212)
@@ -50,6 +50,28 @@ bug_str_cstr_term_char(VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/-test-/string/cstr.c#L50
 }
 
 static VALUE
+bug_str_unterminated_substring(VALUE str, VALUE vbeg, VALUE vlen)
+{
+    long beg = NUM2LONG(vbeg);
+    long len = NUM2LONG(vlen);
+    rb_str_modify(str);
+    if (len < 0) rb_raise(rb_eArgError, "negative length: %ld", len);
+    if (RSTRING_LEN(str) < beg) rb_raise(rb_eIndexError, "beg: %ld", beg);
+    if (RSTRING_LEN(str) < beg + len) rb_raise(rb_eIndexError, "end: %ld", beg + len);
+    str = rb_str_new_shared(str);
+    if (STR_EMBED_P(str)) {
+	RSTRING(str)->basic.flags &= ~RSTRING_EMBED_LEN_MASK;
+	RSTRING(str)->basic.flags |= len << RSTRING_EMBED_LEN_SHIFT;
+	memmove(RSTRING(str)->as.ary, RSTRING(str)->as.ary + beg, len);
+    }
+    else {
+	RSTRING(str)->as.heap.ptr += beg;
+	RSTRING(str)->as.heap.len = len;
+    }
+    return str;
+}
+
+static VALUE
 bug_str_s_cstr_term(VALUE self, VALUE str)
 {
     Check_Type(str, T_STRING);
@@ -114,6 +136,7 @@ Init_cstr(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/-test-/string/cstr.c#L136
     rb_define_method(klass, "cstr_term", bug_str_cstr_term, 0);
     rb_define_method(klass, "cstr_unterm", bug_str_cstr_unterm, 1);
     rb_define_method(klass, "cstr_term_char", bug_str_cstr_term_char, 0);
+    rb_define_method(klass, "unterminated_substring", bug_str_unterminated_substring, 2);
     rb_define_singleton_method(klass, "cstr_term", bug_str_s_cstr_term, 1);
     rb_define_singleton_method(klass, "cstr_unterm", bug_str_s_cstr_unterm, 2);
     rb_define_singleton_method(klass, "cstr_term_char", bug_str_s_cstr_term_char, 1);
Index: test/-ext-/string/test_cstr.rb
===================================================================
--- test/-ext-/string/test_cstr.rb	(revision 55211)
+++ test/-ext-/string/test_cstr.rb	(revision 55212)
@@ -18,6 +18,12 @@ class Test_StringCStr < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/-ext-/string/test_cstr.rb#L18
     assert_equal(0, s.cstr_term, Bug4319)
   end
 
+  def test_shared
+    s = Bug::String.new("abcdef")*5
+    s = s.unterminated_substring(0, 29)
+    assert_equal(0, s.cstr_term, Bug4319)
+  end
+
   def test_frozen
     s0 = Bug::String.new("abcdefgh"*8)
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55211)
+++ ChangeLog	(revision 55212)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon May 30 16:20:26 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (str_fill_term): return new pointer reallocated by
+	  filling terminator.
+
 Mon May 30 14:54:58 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/stringio/stringio.c (enc_subseq): share the return value and
Index: string.c
===================================================================
--- string.c	(revision 55211)
+++ string.c	(revision 55212)
@@ -2012,8 +2012,9 @@ str_fill_term(VALUE str, char *s, long l https://github.com/ruby/ruby/blob/trunk/string.c#L2012
     }
     else {
 	TERM_FILL(s + len, termlen);
+	return s;
     }
-    return s;
+    return RSTRING_PTR(str);
 }
 
 char *

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]