summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--file.c18
-rw-r--r--test/ruby/test_file_exhaustive.rb12
3 files changed, 31 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 55d08c087a..dd962f8988 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Jul 30 08:51:51 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * file.c (file_expand_path): home directory must be absolute.
+ [ruby-core:31537]
+
Fri Jul 30 08:33:20 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (file_expand_path): should check if could find user.
diff --git a/file.c b/file.c
index 355f8e8a8a..46d75498dc 100644
--- a/file.c
+++ b/file.c
@@ -2851,6 +2851,7 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
tainted = OBJ_TAINTED(fname);
if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */
+ long userlen = 0;
tainted = 1;
if (isdirsep(s[1]) || s[1] == '\0') {
buf = 0;
@@ -2859,15 +2860,24 @@ file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
}
else {
s = nextdirsep(b = s);
- BUFCHECK(bdiff + (s-b) >= buflen);
- memcpy(p, b, s-b);
- rb_str_set_len(result, s-b);
+ userlen = s - b;
+ BUFCHECK(bdiff + userlen >= buflen);
+ memcpy(p, b, userlen);
+ rb_str_set_len(result, userlen);
buf = p + 1;
- p += s-b;
+ p += userlen;
}
if (NIL_P(rb_home_dir(buf, result))) {
rb_raise(rb_eArgError, "can't find user %s", buf);
}
+ if (!rb_is_absolute_path(RSTRING_PTR(result))) {
+ if (userlen) {
+ rb_raise(rb_eArgError, "non-absolute home of %.*s", (int)userlen, b);
+ }
+ else {
+ rb_raise(rb_eArgError, "non-absolute home");
+ }
+ }
BUFINIT();
p = pend;
}
diff --git a/test/ruby/test_file_exhaustive.rb b/test/ruby/test_file_exhaustive.rb
index ee4df6b154..5344494679 100644
--- a/test/ruby/test_file_exhaustive.rb
+++ b/test/ruby/test_file_exhaustive.rb
@@ -397,6 +397,18 @@ class TestFileExhaustive < Test::Unit::TestCase
assert_kind_of(String, File.expand_path("~"))
assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha") }
assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha", "/") }
+ begin
+ bug3630 = '[ruby-core:31537]'
+ home = ENV["HOME"]
+ ENV["HOME"] = nil
+ assert_raise(ArgumentError) { File.expand_path("~") }
+ ENV["HOME"] = "~"
+ assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
+ ENV["HOME"] = "."
+ assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
+ ensure
+ ENV["HOME"] = home
+ end
assert_incompatible_encoding {|d| File.expand_path(d)}
end