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

ruby-changes:35317

From: usa <ko1@a...>
Date: Fri, 5 Sep 2014 13:23:54 +0900 (JST)
Subject: [ruby-changes:35317] usa:r47399 (ruby_2_0_0): merge revision(s) 46408, 46410, 46413, 46414, 46424, 46436, 46437: [Backport #9934]

usa	2014-09-05 13:23:37 +0900 (Fri, 05 Sep 2014)

  New Revision: 47399

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

  Log:
    merge revision(s) 46408,46410,46413,46414,46424,46436,46437: [Backport #9934]
    
    string.c: shrink too big buffer
    
    * string.c (rb_str_resize): shrink the buffer even if new length
      is same but it is enough smaller than the capacity.
    * file.c (expand_path): shrink expanded path which no longer needs
      rooms to append.  [ruby-core:63114] [Bug #9934]
    
    * string.c (rb_str_resize): should consider the capacity instead
      of the old length, as pointed out by nagachika.
    
    * string.c (rb_str_resize): update capa only when buffer get
      reallocated.
      http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413

  Modified directories:
    branches/ruby_2_0_0/
  Modified files:
    branches/ruby_2_0_0/ChangeLog
    branches/ruby_2_0_0/file.c
    branches/ruby_2_0_0/string.c
    branches/ruby_2_0_0/test/ruby/test_file_exhaustive.rb
    branches/ruby_2_0_0/version.h
Index: ruby_2_0_0/ChangeLog
===================================================================
--- ruby_2_0_0/ChangeLog	(revision 47398)
+++ ruby_2_0_0/ChangeLog	(revision 47399)
@@ -1,3 +1,19 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1
+Fri Sep  5 13:06:53 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_resize): update capa only when buffer get
+	  reallocated.
+	  http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413
+
+Fri Sep  5 13:06:53 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_resize): should consider the capacity instead
+	  of the old length, as pointed out by nagachika.
+
+Fri Sep  5 13:06:53 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* file.c (expand_path): shrink expanded path which no longer needs
+	  rooms to append.  [ruby-core:63114] [Bug #9934]
+
 Wed Sep  3 13:42:24 2014  Mark Lorenz  <mlorenz@c...>
 
 	* lib/erb.rb (result): [DOC] no longer accepts a Proc, as
Index: ruby_2_0_0/string.c
===================================================================
--- ruby_2_0_0/string.c	(revision 47398)
+++ ruby_2_0_0/string.c	(revision 47399)
@@ -838,7 +838,7 @@ RUBY_FUNC_EXPORTED size_t https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/string.c#L838
 rb_str_memsize(VALUE str)
 {
     if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
-	return RSTRING(str)->as.heap.aux.capa;
+	return RSTRING(str)->as.heap.aux.capa + 1; /* termlen */
     }
     else {
 	return 0;
@@ -1863,9 +1863,11 @@ rb_str_resize(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/string.c#L1863
     independent = str_independent(str);
     ENC_CODERANGE_CLEAR(str);
     slen = RSTRING_LEN(str);
-    if (len != slen) {
+    {
+	long capa;
 	if (STR_EMBED_P(str)) {
-	    if (len <= RSTRING_EMBED_LEN_MAX) {
+	    if (len == slen) return str;
+	    if (len + 1 <= RSTRING_EMBED_LEN_MAX + 1) {
 		STR_SET_EMBED_LEN(str, len);
 		RSTRING(str)->as.ary[len] = '\0';
 		return str;
@@ -1884,14 +1886,15 @@ rb_str_resize(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/string.c#L1886
 	    return str;
 	}
 	else if (!independent) {
+	    if (len == slen) return str;
 	    str_make_independent_expand(str, len - slen);
 	}
-	else if (slen < len || slen - len > 1024) {
+	else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
+		 (capa - len) > (len < 1024 ? len : 1024)) {
 	    REALLOC_N(RSTRING(str)->as.heap.ptr, char, len+1);
-	}
-	if (!STR_NOCAPA_P(str)) {
 	    RSTRING(str)->as.heap.aux.capa = len;
 	}
+	else if (len == slen) return str;
 	RSTRING(str)->as.heap.len = len;
 	RSTRING(str)->as.heap.ptr[len] = '\0';	/* sentinel */
     }
Index: ruby_2_0_0/version.h
===================================================================
--- ruby_2_0_0/version.h	(revision 47398)
+++ ruby_2_0_0/version.h	(revision 47399)
@@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1
 #define RUBY_VERSION "2.0.0"
-#define RUBY_RELEASE_DATE "2014-09-03"
-#define RUBY_PATCHLEVEL 548
+#define RUBY_RELEASE_DATE "2014-09-05"
+#define RUBY_PATCHLEVEL 549
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 9
-#define RUBY_RELEASE_DAY 3
+#define RUBY_RELEASE_DAY 5
 
 #include "ruby/version.h"
 
Index: ruby_2_0_0/test/ruby/test_file_exhaustive.rb
===================================================================
--- ruby_2_0_0/test/ruby/test_file_exhaustive.rb	(revision 47398)
+++ ruby_2_0_0/test/ruby/test_file_exhaustive.rb	(revision 47399)
@@ -453,6 +453,15 @@ class TestFileExhaustive < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_file_exhaustive.rb#L453
     end
   end
 
+  def test_expand_path_memsize
+    bug9934 = '[ruby-core:63114] [Bug #9934]'
+    require "objspace"
+    path = File.expand_path("/foo")
+    assert_operator(ObjectSpace.memsize_of(path), :<=, path.bytesize, bug9934)
+    path = File.expand_path("/a"*25)
+    assert_equal(path.bytesize+1, ObjectSpace.memsize_of(path), bug9934)
+  end
+
   def test_expand_path_encoding
     drive = (DRIVE ? 'C:' : '')
     if Encoding.find("filesystem") == Encoding::CP1251
Index: ruby_2_0_0/file.c
===================================================================
--- ruby_2_0_0/file.c	(revision 47398)
+++ ruby_2_0_0/file.c	(revision 47399)
@@ -3301,6 +3301,16 @@ rb_file_expand_path_internal(VALUE fname https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/file.c#L3301
 
 #define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2)
 
+static VALUE
+str_shrink(VALUE str)
+{
+    rb_str_resize(str, RSTRING_LEN(str));
+    return str;
+}
+
+#define expand_path(fname, dname, abs_mode, long_name, result) \
+    str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
+
 #define check_expand_path_args(fname, dname) \
     (((fname) = rb_get_path(fname)), \
      (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
@@ -3315,13 +3325,13 @@ VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/file.c#L3325
 rb_file_expand_path(VALUE fname, VALUE dname)
 {
     check_expand_path_args(fname, dname);
-    return rb_file_expand_path_internal(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
+    return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
 }
 
 VALUE
 rb_file_expand_path_fast(VALUE fname, VALUE dname)
 {
-    return rb_file_expand_path_internal(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
+    return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
 }
 
 /*
@@ -3358,7 +3368,7 @@ VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/file.c#L3368
 rb_file_absolute_path(VALUE fname, VALUE dname)
 {
     check_expand_path_args(fname, dname);
-    return rb_file_expand_path_internal(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
+    return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
 }
 
 /*
@@ -5287,6 +5297,7 @@ is_explicit_relative(const char *path) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/file.c#L5297
 static VALUE
 copy_path_class(VALUE path, VALUE orig)
 {
+    str_shrink(path);
     RBASIC(path)->klass = rb_obj_class(orig);
     OBJ_FREEZE(path);
     return path;

Property changes on: ruby_2_0_0
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r46408,46410,46413-46414,46424,46436-46437


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

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