summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-05 15:39:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-08-05 15:39:46 +0000
commite70df3a701d01a54df5e9b665415c7f3a86779c1 (patch)
treefd6add9be2ebdecf9ead089f3e6298ec2b1e844a
parent549605c15dd30252fbdf777f719bd9572a5e3b92 (diff)
hash.c: env_name_new
* hash.c (env_enc_str_new): make string for an environment variable name or value. * hash.c (env_name_new): make environment value string with the encoding for its name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55819 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--hash.c53
-rw-r--r--test/ruby/test_env.rb3
3 files changed, 43 insertions, 21 deletions
diff --git a/ChangeLog b/ChangeLog
index 83fcd53..7337862 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Sat Aug 6 00:39:44 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * hash.c (env_enc_str_new): make string for an environment
+ variable name or value.
+
+ * hash.c (env_name_new): make environment value string with the
+ encoding for its name.
+
Fri Aug 5 23:18:35 2016 NAKAMURA Usaku <usa@ruby-lang.org>
* hasn.c (env_str_new): taint the string. get rid of a test failure
diff --git a/hash.c b/hash.c
index 1474460..d2e0591 100644
--- a/hash.c
+++ b/hash.c
@@ -2940,30 +2940,29 @@ env_str_transcode(VALUE str, rb_encoding *enc)
#endif
static VALUE
-env_str_new(const char *ptr, long len)
+env_enc_str_new(const char *ptr, long len, rb_encoding *enc)
{
#ifdef _WIN32
- VALUE str = env_str_transcode(rb_utf8_str_new(ptr, len), rb_locale_encoding());
- OBJ_TAINT(str); /* rb_locale_str_new makes tainted string, but rb_utf8_str_new doesn't */
+ VALUE str = env_str_transcode(rb_utf8_str_new(ptr, len), enc);
#else
- VALUE str = rb_locale_str_new(ptr, len);
+ VALUE str = rb_external_str_new_with_enc(ptr, len, enc);
#endif
+ OBJ_TAINT(str);
rb_obj_freeze(str);
return str;
}
static VALUE
-env_path_str_new(const char *ptr)
+env_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
{
-#ifdef _WIN32
- VALUE str = env_str_transcode(rb_utf8_str_new_cstr(ptr), rb_filesystem_encoding());
-#else
- VALUE str = rb_filesystem_str_new_cstr(ptr);
-#endif
+ return env_enc_str_new(ptr, strlen(ptr), enc);
+}
- rb_obj_freeze(str);
- return str;
+static VALUE
+env_str_new(const char *ptr, long len)
+{
+ return env_enc_str_new(ptr, len, rb_locale_encoding());
}
static VALUE
@@ -2973,6 +2972,25 @@ env_str_new2(const char *ptr)
return env_str_new(ptr, strlen(ptr));
}
+static int env_path_tainted(const char *);
+
+static rb_encoding *
+env_encoding_for(const char *name, const char *ptr)
+{
+ if (ENVMATCH(name, PATH_ENV) && !env_path_tainted(ptr)) {
+ return rb_filesystem_encoding();
+ }
+ else {
+ return rb_locale_encoding();
+ }
+}
+
+static VALUE
+env_name_new(const char *name, const char *ptr)
+{
+ return env_enc_str_new_cstr(ptr, env_encoding_for(name, ptr));
+}
+
static void *
get_env_cstr(
#ifdef _WIN32
@@ -3061,8 +3079,6 @@ env_delete_m(VALUE obj, VALUE name)
return val;
}
-static int env_path_tainted(const char *);
-
/*
* call-seq:
* ENV[name] -> value
@@ -3078,10 +3094,7 @@ rb_f_getenv(VALUE obj, VALUE name)
nam = env_name(name);
env = getenv(nam);
if (env) {
- if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env)) {
- return env_path_str_new(env);
- }
- return env_str_new2(env);
+ return env_name_new(nam, env);
}
return Qnil;
}
@@ -3122,9 +3135,7 @@ env_fetch(int argc, VALUE *argv)
}
return argv[1];
}
- if (ENVMATCH(nam, PATH_ENV) && !env_path_tainted(env))
- return env_path_str_new(env);
- return env_str_new2(env);
+ return env_name_new(nam, env);
}
static void
diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb
index 77e8386..29c0583 100644
--- a/test/ruby/test_env.rb
+++ b/test/ruby/test_env.rb
@@ -46,6 +46,7 @@ class TestEnv < Test::Unit::TestCase
end
ENV['TEST'] = 'bar'
assert_equal('bar', ENV['TEST'])
+ assert_predicate(ENV['TEST'], :tainted?)
if IGNORE_CASE
assert_equal('bar', ENV['test'])
else
@@ -112,6 +113,7 @@ class TestEnv < Test::Unit::TestCase
assert_invalid_env {|v| ENV[v]}
ENV[PATH_ENV] = ""
assert_equal("", ENV[PATH_ENV])
+ assert_predicate(ENV[PATH_ENV], :tainted?)
assert_nil(ENV[""])
end
@@ -130,6 +132,7 @@ class TestEnv < Test::Unit::TestCase
assert_nothing_raised { ENV.fetch(PATH_ENV, "foo") }
ENV[PATH_ENV] = ""
assert_equal("", ENV.fetch(PATH_ENV))
+ assert_predicate(ENV.fetch(PATH_ENV), :tainted?)
end
def test_aset