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
This commit is contained in:
usa 2014-09-05 04:23:37 +00:00
parent d7f49a9b06
commit b9e630607e
5 changed files with 51 additions and 12 deletions

View file

@ -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

17
file.c
View file

@ -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;

View file

@ -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 */
}

View file

@ -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

View file

@ -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"