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

ruby-changes:39291

From: nobu <ko1@a...>
Date: Sat, 25 Jul 2015 11:05:47 +0900 (JST)
Subject: [ruby-changes:39291] nobu:r51372 (trunk): string.c: fill the terminator

nobu	2015-07-25 11:05:34 +0900 (Sat, 25 Jul 2015)

  New Revision: 51372

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

  Log:
    string.c: fill the terminator
    
    * string.c (str_replace_shared_without_enc): fill the terminator
      of embedded strings in wide char encodings.

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/-ext-/string/test_cstr.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51371)
+++ ChangeLog	(revision 51372)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jul 25 11:05:31 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (str_replace_shared_without_enc): fill the terminator
+	  of embedded strings in wide char encodings.
+
 Sat Jul 25 06:38:36 2015  Koichi Sasada  <ko1@a...>
 
 	* vm_core.h: size should be unsigned.
Index: string.c
===================================================================
--- string.c	(revision 51371)
+++ string.c	(revision 51372)
@@ -962,16 +962,24 @@ rb_str_export_to_enc(VALUE str, rb_encod https://github.com/ruby/ruby/blob/trunk/string.c#L962
 static VALUE
 str_replace_shared_without_enc(VALUE str2, VALUE str)
 {
-    if (RSTRING_LEN(str) <= RSTRING_EMBED_LEN_MAX) {
+    const int termlen = TERM_LEN(str);
+    char *ptr;
+    long len;
+
+    RSTRING_GETMEM(str, ptr, len);
+    if (len+termlen <= RSTRING_EMBED_LEN_MAX+1) {
+	char *ptr2 = RSTRING(str2)->as.ary;
 	STR_SET_EMBED(str2);
-	memcpy(RSTRING_PTR(str2), RSTRING_PTR(str), RSTRING_LEN(str)+1);
-	STR_SET_EMBED_LEN(str2, RSTRING_LEN(str));
+	memcpy(ptr2, RSTRING_PTR(str), len);
+	STR_SET_EMBED_LEN(str2, len);
+	TERM_FILL(ptr2+len, termlen);
     }
     else {
 	str = rb_str_new_frozen(str);
 	FL_SET(str2, STR_NOEMBED);
-	RSTRING(str2)->as.heap.len = RSTRING_LEN(str);
-	RSTRING(str2)->as.heap.ptr = RSTRING_PTR(str);
+	RSTRING_GETMEM(str, ptr, len);
+	RSTRING(str2)->as.heap.len = len;
+	RSTRING(str2)->as.heap.ptr = ptr;
 	STR_SET_SHARED(str2, str);
     }
     return str2;
Index: test/-ext-/string/test_cstr.rb
===================================================================
--- test/-ext-/string/test_cstr.rb	(revision 51371)
+++ test/-ext-/string/test_cstr.rb	(revision 51372)
@@ -86,6 +86,14 @@ class Test_StringCStr < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/-ext-/string/test_cstr.rb#L86
     }
   end
 
+  def test_wchar_replace
+    assert_wchars_term_char("abc") {|s|
+      w = s.dup
+      s.replace("abcdefghijklmnop")
+      s.replace(w)
+    }
+  end
+
   def test_embedded_from_heap
     gh821 = "[GH-821]"
     embedded_string = "abcdefghi"

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

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