Skip to content

Commit abacb94

Browse files
nobutmm1
authored andcommitted
file.c: shrink expanded path
* file.c (expand_path): shrink expanded path which no longer needs rooms to append. [ruby-core:63114] [Bug ruby#9934] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46410 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog
1 parent 2aa6072 commit abacb94

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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;

test/ruby/test_file_exhaustive.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@ 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+
end
467+
461468
def test_expand_path_encoding
462469
drive = (DRIVE ? 'C:' : '')
463470
if Encoding.find("filesystem") == Encoding::CP1251

0 commit comments

Comments
 (0)