summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-05 04:23:37 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-05 04:23:37 +0000
commitb9e630607ed20bd35adf75faee325d88654c42e4 (patch)
tree8415d744ce43c8bfd2c73a2e0a32231a5a24d896
parentd7f49a9b066f511cdea83762b81d25fffd2f54c7 (diff)
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 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@47399 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog16
-rw-r--r--file.c17
-rw-r--r--string.c15
-rw-r--r--test/ruby/test_file_exhaustive.rb9
-rw-r--r--version.h6
5 files changed, 51 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index 8fe477315c..bb4d9a11df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+Fri Sep 5 13:06:53 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
+
+ * 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@ruby-lang.org>
+
+ * 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@covermymeds.com>
* lib/erb.rb (result): [DOC] no longer accepts a Proc, as
diff --git a/file.c b/file.c
index 70da79b735..96544766a9 100644
--- a/file.c
+++ b/file.c
@@ -3301,6 +3301,16 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
#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
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
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)
static VALUE
copy_path_class(VALUE path, VALUE orig)
{
+ str_shrink(path);
RBASIC(path)->klass = rb_obj_class(orig);
OBJ_FREEZE(path);
return path;
diff --git a/string.c b/string.c
index a45d7ad471..d6a5232411 100644
--- a/string.c
+++ b/string.c
@@ -838,7 +838,7 @@ RUBY_FUNC_EXPORTED size_t
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)
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)
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 */
}
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index 5f98042c32..3cd56cb4f2 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -453,6 +453,15 @@ class TestFileExhaustive < Test::Unit::TestCase
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
diff --git a/version.h b/version.h
index a55fcf912b..c06ad61d4d 100644
--- a/version.h
+++ b/version.h
@@ -1,10 +1,10 @@
#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"