summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-06-10 05:48:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-06-10 05:48:38 +0000
commit2667d1b38f39390d9c3bb90c5257ffb9723e1c77 (patch)
treecc4ef8e453688521d56d655699ed4900bc7b4ea4
parentf9843bc4dcc9b7313ed5a631390c502a894f2f82 (diff)
hash.c: ensure NUL-terminated for ENV
* hash.c (get_env_cstr): ensure NUL-terminated. [ruby-dev:49655] [Bug #12475] * string.c (rb_str_fill_terminator): return the pointer to the NUL-terminated content. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55345 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--hash.c2
-rw-r--r--internal.h2
-rw-r--r--string.c4
-rw-r--r--test/ruby/test_env.rb14
5 files changed, 26 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 98bdc744b54..e5a3f039bf9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Jun 10 14:48:36 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * hash.c (get_env_cstr): ensure NUL-terminated.
+ [ruby-dev:49655] [Bug #12475]
+
+ * string.c (rb_str_fill_terminator): return the pointer to the
+ NUL-terminated content.
+
Thu Jun 9 21:42:00 2016 Kazuki Yamaguchi <k@rhe.jp>
* ext/openssl/ossl_asn1.c (asn1integer_to_num): Use
diff --git a/hash.c b/hash.c
index 503ff174ac7..561aa904008 100644
--- a/hash.c
+++ b/hash.c
@@ -2971,7 +2971,7 @@ get_env_cstr(
if (memchr(var, '\0', RSTRING_LEN(str))) {
rb_raise(rb_eArgError, "bad environment variable %s: contains null byte", name);
}
- return var;
+ return rb_str_fill_terminator(str, 1); /* ASCII compatible */
}
#ifdef _WIN32
diff --git a/internal.h b/internal.h
index 1294b8d091a..dc35d31bbdd 100644
--- a/internal.h
+++ b/internal.h
@@ -1350,7 +1350,7 @@ VALUE rb_str_quote_unprintable(VALUE);
VALUE rb_id_quote_unprintable(ID);
#define QUOTE(str) rb_str_quote_unprintable(str)
#define QUOTE_ID(id) rb_id_quote_unprintable(id)
-void rb_str_fill_terminator(VALUE str, const int termlen);
+char *rb_str_fill_terminator(VALUE str, const int termlen);
VALUE rb_str_locktmp_ensure(VALUE str, VALUE (*func)(VALUE), VALUE arg);
#ifdef RUBY_ENCODING_H
VALUE rb_external_str_with_enc(VALUE str, rb_encoding *eenc);
diff --git a/string.c b/string.c
index 776a4d32ae0..09afe05cb13 100644
--- a/string.c
+++ b/string.c
@@ -2048,12 +2048,12 @@ rb_string_value_cstr(volatile VALUE *ptr)
return s;
}
-void
+char *
rb_str_fill_terminator(VALUE str, const int newminlen)
{
char *s = RSTRING_PTR(str);
long len = RSTRING_LEN(str);
- str_fill_term(str, s, len, newminlen);
+ return str_fill_term(str, s, len, newminlen);
}
VALUE
diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb
index b14ea229962..17323ccf956 100644
--- a/test/ruby/test_env.rb
+++ b/test/ruby/test_env.rb
@@ -455,6 +455,20 @@ class TestEnv < Test::Unit::TestCase
end
end
+ def test_shared_substring
+ bug12475 = '[ruby-dev:49655] [Bug #12475]'
+ n = [*"0".."9"].join("")*3
+ e0 = ENV[n0 = "E#{n}"]
+ e1 = ENV[n1 = "E#{n}."]
+ ENV[n0] = nil
+ ENV[n1] = nil
+ ENV[n1.chop] = "T#{n}.".chop
+ ENV[n0], e0 = e0, ENV[n0]
+ ENV[n1], e1 = e1, ENV[n1]
+ assert_equal("T#{n}", e0, bug12475)
+ assert_nil(e1, bug12475)
+ end
+
if RUBY_PLATFORM =~ /bccwin|mswin|mingw/
def test_memory_leak_aset
bug9977 = '[ruby-dev:48323] [Bug #9977]'