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

ruby-changes:43482

From: ngoto <ko1@a...>
Date: Fri, 1 Jul 2016 20:24:17 +0900 (JST)
Subject: [ruby-changes:43482] ngoto:r55555 (trunk): * string.c: Specify termlen as far as possible.

ngoto	2016-07-01 20:24:11 +0900 (Fri, 01 Jul 2016)

  New Revision: 55555

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

  Log:
    * string.c: Specify termlen as far as possible.
      Additional fix for [Bug #12536] [ruby-dev:49699].
    
    * string.c (rb_usascii_str_new, rb_utf8_str_new): Specify termlen
      which is apparently 1 for the encodings.
    
    * string.c (str_new0_cstr): New static function to create a String
      object from a C string with specifying termlen.
    
    * string.c (rb_usascii_str_new_cstr, rb_utf8_str_new_cstr): Specify
      termlen by using new str_new0_cstr().
    
    * string.c (str_new_static): Specify termlen from the given encoding
      when creating a new String object is needed.
    
    * string.c (rb_tainted_str_new_with_enc): New function to create a
      tainted String object with the given encoding. This means that
      the termlen is correctly specified. Curretly static function.
      The function name might be renamed to rb_tainted_enc_str_new
      or rb_enc_tainted_str_new.
    
    * string.c (rb_external_str_new_with_enc): Use encoding by using the
      above rb_tainted_str_new_with_enc().

  Modified files:
    trunk/ChangeLog
    trunk/string.c
Index: string.c
===================================================================
--- string.c	(revision 55554)
+++ string.c	(revision 55555)
@@ -726,7 +726,7 @@ rb_str_new(const char *ptr, long len) https://github.com/ruby/ruby/blob/trunk/string.c#L726
 VALUE
 rb_usascii_str_new(const char *ptr, long len)
 {
-    VALUE str = rb_str_new(ptr, len);
+    VALUE str = str_new0(rb_cString, ptr, len, 1); /* termlen == 1 */
     ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
     return str;
 }
@@ -734,7 +734,7 @@ rb_usascii_str_new(const char *ptr, long https://github.com/ruby/ruby/blob/trunk/string.c#L734
 VALUE
 rb_utf8_str_new(const char *ptr, long len)
 {
-    VALUE str = str_new(rb_cString, ptr, len);
+    VALUE str = str_new0(rb_cString, ptr, len, 1); /* termlen == 1 */
     rb_enc_associate_index(str, rb_utf8_encindex());
     return str;
 }
@@ -758,10 +758,17 @@ rb_str_new_cstr(const char *ptr) https://github.com/ruby/ruby/blob/trunk/string.c#L758
     return rb_str_new(ptr, strlen(ptr));
 }
 
+static VALUE
+str_new0_cstr(const char *ptr, int termlen)
+{
+    must_not_null(ptr);
+    return str_new0(rb_cString, ptr, strlen(ptr), termlen);
+}
+
 VALUE
 rb_usascii_str_new_cstr(const char *ptr)
 {
-    VALUE str = rb_str_new_cstr(ptr);
+    VALUE str = str_new0_cstr(ptr, 1); /* termlen == 1 */
     ENCODING_CODERANGE_SET(str, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
     return str;
 }
@@ -769,7 +776,7 @@ rb_usascii_str_new_cstr(const char *ptr) https://github.com/ruby/ruby/blob/trunk/string.c#L776
 VALUE
 rb_utf8_str_new_cstr(const char *ptr)
 {
-    VALUE str = rb_str_new_cstr(ptr);
+    VALUE str = str_new0_cstr(ptr, 1); /* termlen == 1 */
     rb_enc_associate_index(str, rb_utf8_encindex());
     return str;
 }
@@ -794,7 +801,8 @@ str_new_static(VALUE klass, const char * https://github.com/ruby/ruby/blob/trunk/string.c#L801
     }
 
     if (!ptr) {
-	str = str_new(klass, ptr, len);
+	rb_encoding *enc = rb_enc_get_from_index(encindex);
+	str = str_new0(klass, ptr, len, rb_enc_mbminlen(enc));
     }
     else {
 	RUBY_DTRACE_CREATE_HOOK(STRING, len);
@@ -842,6 +850,15 @@ rb_tainted_str_new(const char *ptr, long https://github.com/ruby/ruby/blob/trunk/string.c#L850
     return str;
 }
 
+static VALUE
+rb_tainted_str_new_with_enc(const char *ptr, long len, rb_encoding *enc)
+{
+    VALUE str = rb_enc_str_new(ptr, len, enc);
+
+    OBJ_TAINT(str);
+    return str;
+}
+
 VALUE
 rb_tainted_str_new_cstr(const char *ptr)
 {
@@ -974,7 +991,7 @@ rb_external_str_new_with_enc(const char https://github.com/ruby/ruby/blob/trunk/string.c#L991
 {
     VALUE str;
 
-    str = rb_tainted_str_new(ptr, len);
+    str = rb_tainted_str_new_with_enc(ptr, len, eenc);
     return rb_external_str_with_enc(str, eenc);
 }
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55554)
+++ ChangeLog	(revision 55555)
@@ -1,3 +1,29 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jul  1 20:20:20 2016  Naohisa Goto  <ngotogenome@g...>
+
+  	* string.c: Specify termlen as far as possible.
+	  Additional fix for [Bug #12536] [ruby-dev:49699].
+
+	* string.c (rb_usascii_str_new, rb_utf8_str_new): Specify termlen
+	  which is apparently 1 for the encodings.
+
+	* string.c (str_new0_cstr): New static function to create a String
+	  object from a C string with specifying termlen.
+
+	* string.c (rb_usascii_str_new_cstr, rb_utf8_str_new_cstr): Specify
+	  termlen by using new str_new0_cstr().
+
+	* string.c (str_new_static): Specify termlen from the given encoding
+	  when creating a new String object is needed.
+
+	* string.c (rb_tainted_str_new_with_enc): New function to create a
+	  tainted String object with the given encoding. This means that
+	  the termlen is correctly specified. Curretly static function.
+	  The function name might be renamed to rb_tainted_enc_str_new
+	  or rb_enc_tainted_str_new.
+
+	* string.c (rb_external_str_new_with_enc): Use encoding by using the
+	  above rb_tainted_str_new_with_enc().
+
 Fri Jul  1 19:38:57 2016  Naohisa Goto  <ngotogenome@g...>
 
 	* test/fiddle/test_pointer.rb (test_to_str, test_to_s, test_aref_aset):

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

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