Skip to content

Commit 4198f14

Browse files
committed
hash.c: fix memory leak
* hash.c (ruby_setenv): fix memory leak on Windows, free environment strings block after check for the size. [ruby-dev:48323] [Bug ruby#9977] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46550 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent e1884ec commit 4198f14

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Wed Jun 25 22:31:32 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* hash.c (ruby_setenv): fix memory leak on Windows, free
4+
environment strings block after check for the size.
5+
[ruby-dev:48323] [Bug #9977]
6+
17
Wed Jun 25 15:44:12 2014 Eric Wong <[email protected]>
28

39
* ccan/container_of/container_of.h (container_off_var):

hash.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,9 +2750,12 @@ ruby_setenv(const char *name, const char *value)
27502750
int failed = 0;
27512751
check_envname(name);
27522752
if (value) {
2753-
const char* p = GetEnvironmentStringsA();
2753+
char* p = GetEnvironmentStringsA();
2754+
size_t n;
27542755
if (!p) goto fail; /* never happen */
2755-
if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) {
2756+
n = strlen(name) + 2 + strlen(value) + getenvsize(p);
2757+
FreeEnvironmentStringsA(p);
2758+
if (n >= getenvblocksize()) {
27562759
goto fail; /* 2 for '=' & '\0' */
27572760
}
27582761
buf = rb_sprintf("%s=%s", name, value);

test/ruby/test_env.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'test/unit'
2+
require_relative 'envutil'
23

34
class TestEnv < Test::Unit::TestCase
45
IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM
@@ -507,4 +508,13 @@ def test_taint_aset_key
507508
end.call
508509
end
509510
end
511+
512+
def test_memory_leak_aset
513+
bug9977 = '[ruby-dev:48323] [Bug #9977]'
514+
assert_no_memory_leak([], <<-'end;', "5_000.times {ENV[k] = v}", bug9977)
515+
ENV.clear
516+
k = 'FOO'
517+
v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500)
518+
end;
519+
end
510520
end

0 commit comments

Comments
 (0)