Skip to content

Commit 659fd08

Browse files
committed
merge revision(s) r46408,r46410,r46413,r46414,r46424,r46436,r46437: [Backport ruby#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 ruby#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_1@47215 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent c65eb7b commit 659fd08

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

ChangeLog

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
Mon Aug 18 23:22:03 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* string.c (rb_str_resize): update capa only when buffer get
4+
reallocated.
5+
http://d.hatena.ne.jp/nagachika/20140613/ruby_trunk_changes_46413_46420#r46413
6+
7+
Mon Aug 18 23:22:03 2014 Nobuyoshi Nakada <[email protected]>
8+
9+
* string.c (rb_str_resize): should consider the capacity instead
10+
of the old length, as pointed out by nagachika.
11+
12+
Mon Aug 18 23:22:03 2014 Nobuyoshi Nakada <[email protected]>
13+
14+
* file.c (expand_path): shrink expanded path which no longer needs
15+
rooms to append. [ruby-core:63114] [Bug #9934]
16+
117
Mon Aug 11 23:55:32 2014 Mark Lorenz <[email protected]>
218

319
* lib/erb.rb (result): [DOC] no longer accepts a Proc, as

file.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3397,6 +3397,16 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
33973397

33983398
#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2)
33993399

3400+
static VALUE
3401+
str_shrink(VALUE str)
3402+
{
3403+
rb_str_resize(str, RSTRING_LEN(str));
3404+
return str;
3405+
}
3406+
3407+
#define expand_path(fname, dname, abs_mode, long_name, result) \
3408+
str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
3409+
34003410
#define check_expand_path_args(fname, dname) \
34013411
(((fname) = rb_get_path(fname)), \
34023412
(void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
@@ -3411,13 +3421,13 @@ VALUE
34113421
rb_file_expand_path(VALUE fname, VALUE dname)
34123422
{
34133423
check_expand_path_args(fname, dname);
3414-
return rb_file_expand_path_internal(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
3424+
return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
34153425
}
34163426

34173427
VALUE
34183428
rb_file_expand_path_fast(VALUE fname, VALUE dname)
34193429
{
3420-
return rb_file_expand_path_internal(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
3430+
return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
34213431
}
34223432

34233433
/*
@@ -3465,7 +3475,7 @@ VALUE
34653475
rb_file_absolute_path(VALUE fname, VALUE dname)
34663476
{
34673477
check_expand_path_args(fname, dname);
3468-
return rb_file_expand_path_internal(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
3478+
return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
34693479
}
34703480

34713481
/*
@@ -5408,6 +5418,7 @@ is_explicit_relative(const char *path)
54085418
static VALUE
54095419
copy_path_class(VALUE path, VALUE orig)
54105420
{
5421+
str_shrink(path);
54115422
RBASIC_SET_CLASS(path, rb_obj_class(orig));
54125423
OBJ_FREEZE(path);
54135424
return path;

string.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,9 +2034,11 @@ rb_str_resize(VALUE str, long len)
20342034
independent = str_independent(str);
20352035
ENC_CODERANGE_CLEAR(str);
20362036
slen = RSTRING_LEN(str);
2037-
if (len != slen) {
2037+
{
2038+
long capa;
20382039
const int termlen = TERM_LEN(str);
20392040
if (STR_EMBED_P(str)) {
2041+
if (len == slen) return str;
20402042
if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) {
20412043
STR_SET_EMBED_LEN(str, len);
20422044
TERM_FILL(RSTRING(str)->as.ary + len, termlen);
@@ -2056,14 +2058,15 @@ rb_str_resize(VALUE str, long len)
20562058
return str;
20572059
}
20582060
else if (!independent) {
2061+
if (len == slen) return str;
20592062
str_make_independent_expand(str, len - slen);
20602063
}
2061-
else if (slen < len || slen - len > 1024) {
2064+
else if ((capa = RSTRING(str)->as.heap.aux.capa) < len ||
2065+
(capa - len) > (len < 1024 ? len : 1024)) {
20622066
REALLOC_N(RSTRING(str)->as.heap.ptr, char, len + termlen);
2063-
}
2064-
if (!STR_NOCAPA_P(str)) {
20652067
RSTRING(str)->as.heap.aux.capa = len;
20662068
}
2069+
else if (len == slen) return str;
20672070
RSTRING(str)->as.heap.len = len;
20682071
TERM_FILL(RSTRING(str)->as.heap.ptr + len, termlen); /* sentinel */
20692072
}

test/ruby/test_file_exhaustive.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,15 @@ def test_expand_path
458458
end
459459
end
460460

461+
def test_expand_path_memsize
462+
bug9934 = '[ruby-core:63114] [Bug #9934]'
463+
require "objspace"
464+
path = File.expand_path("/foo")
465+
assert_operator(ObjectSpace.memsize_of(path), :<=, path.bytesize, bug9934)
466+
path = File.expand_path("/a"*25)
467+
assert_equal(path.bytesize+1, ObjectSpace.memsize_of(path), bug9934)
468+
end
469+
461470
def test_expand_path_encoding
462471
drive = (DRIVE ? 'C:' : '')
463472
if Encoding.find("filesystem") == Encoding::CP1251

version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#define RUBY_VERSION "2.1.2"
2-
#define RUBY_RELEASE_DATE "2014-08-11"
3-
#define RUBY_PATCHLEVEL 200
2+
#define RUBY_RELEASE_DATE "2014-08-18"
3+
#define RUBY_PATCHLEVEL 201
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 8
7-
#define RUBY_RELEASE_DAY 11
7+
#define RUBY_RELEASE_DAY 18
88

99
#include "ruby/version.h"
1010

0 commit comments

Comments
 (0)