summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-25 23:28:04 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-06-25 23:28:04 (GMT)
commit78c763a25c4bf5ddd1dcf5887ad9c94ba27575df (patch)
treee916a18df671be5c20a512db823da652b5100d61 /hash.c
parent7eeaf208b484f257bc6c04e4229cb924e00a18c1 (diff)
hash.c: fix memory leak
* hash.c (env_select): fix memory leak and crash on Windows, make keys array first instead of iterating on envrion directly. [ruby-dev:48325] [Bug #9978] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46557 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/hash.c b/hash.c
index 56cedf0..bcf6b5e 100644
--- a/hash.c
+++ b/hash.c
@@ -3138,23 +3138,21 @@ static VALUE
env_select(VALUE ehash)
{
VALUE result;
- char **env;
+ VALUE keys;
+ long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
result = rb_hash_new();
- env = GET_ENVIRON(environ);
- while (*env) {
- char *s = strchr(*env, '=');
- if (s) {
- VALUE k = env_str_new(*env, s-*env);
- VALUE v = env_str_new2(s+1);
- if (RTEST(rb_yield_values(2, k, v))) {
- rb_hash_aset(result, k, v);
+ keys = env_keys();
+ for (i = 0; i < RARRAY_LEN(keys); ++i) {
+ VALUE key = RARRAY_AREF(keys, i);
+ VALUE val = rb_f_getenv(Qnil, key);
+ if (!NIL_P(val)) {
+ if (RTEST(rb_yield_values(2, key, val))) {
+ rb_hash_aset(result, key, val);
}
}
- env++;
}
- FREE_ENVIRON(environ);
return result;
}