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

ruby-changes:38081

From: nobu <ko1@a...>
Date: Sat, 4 Apr 2015 11:30:43 +0900 (JST)
Subject: [ruby-changes:38081] nobu:r50162 (trunk): string.c: check before modify

nobu	2015-04-04 11:30:26 +0900 (Sat, 04 Apr 2015)

  New Revision: 50162

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

  Log:
    string.c: check before modify
    
    * string.c (rb_str_setbyte): check the argument first not to
      discard shared string and code range unnecessarily until
      actually changing the contents.  pointed out by headius.

  Modified files:
    trunk/ChangeLog
    trunk/string.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50161)
+++ ChangeLog	(revision 50162)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr  4 11:30:24 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_setbyte): check the argument first not to
+	  discard shared string and code range unnecessarily until
+	  actually changing the contents.  pointed out by headius.
+
 Sat Apr  4 08:16:43 2015  NARUSE, Yui  <naruse@r...>
 
 	* lib/net/http.rb (edit_path): use path which is absolute ftp url
Index: string.c
===================================================================
--- string.c	(revision 50161)
+++ string.c	(revision 50162)
@@ -4567,13 +4567,14 @@ rb_str_setbyte(VALUE str, VALUE index, V https://github.com/ruby/ruby/blob/trunk/string.c#L4567
 {
     long pos = NUM2LONG(index);
     int byte = NUM2INT(value);
+    long len = RSTRING_LEN(str);
 
-    rb_str_modify(str);
-
-    if (pos < -RSTRING_LEN(str) || RSTRING_LEN(str) <= pos)
+    if (pos < -len || len <= pos)
         rb_raise(rb_eIndexError, "index %ld out of string", pos);
     if (pos < 0)
-        pos += RSTRING_LEN(str);
+        pos += len;
+
+    rb_str_modify(str);
 
     RSTRING_PTR(str)[pos] = byte;
 

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

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