* hash.c (ruby_setenv): fix memory leak on Windows, free
  environment strings block after check for the size.
  [ruby-dev:48323] [Bug #9977]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46550 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-06-25 13:31:34 +00:00
Родитель e1884ec820
Коммит 4198f1473a
3 изменённых файлов: 21 добавлений и 2 удалений

Просмотреть файл

@ -1,3 +1,9 @@
Wed Jun 25 22:31:32 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* hash.c (ruby_setenv): fix memory leak on Windows, free
environment strings block after check for the size.
[ruby-dev:48323] [Bug #9977]
Wed Jun 25 15:44:12 2014 Eric Wong <e@80x24.org> Wed Jun 25 15:44:12 2014 Eric Wong <e@80x24.org>
* ccan/container_of/container_of.h (container_off_var): * ccan/container_of/container_of.h (container_off_var):

7
hash.c
Просмотреть файл

@ -2750,9 +2750,12 @@ ruby_setenv(const char *name, const char *value)
int failed = 0; int failed = 0;
check_envname(name); check_envname(name);
if (value) { if (value) {
const char* p = GetEnvironmentStringsA(); char* p = GetEnvironmentStringsA();
size_t n;
if (!p) goto fail; /* never happen */ if (!p) goto fail; /* never happen */
if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) { n = strlen(name) + 2 + strlen(value) + getenvsize(p);
FreeEnvironmentStringsA(p);
if (n >= getenvblocksize()) {
goto fail; /* 2 for '=' & '\0' */ goto fail; /* 2 for '=' & '\0' */
} }
buf = rb_sprintf("%s=%s", name, value); buf = rb_sprintf("%s=%s", name, value);

Просмотреть файл

@ -1,4 +1,5 @@
require 'test/unit' require 'test/unit'
require_relative 'envutil'
class TestEnv < Test::Unit::TestCase class TestEnv < Test::Unit::TestCase
IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM
@ -507,4 +508,13 @@ class TestEnv < Test::Unit::TestCase
end.call end.call
end end
end end
def test_memory_leak_aset
bug9977 = '[ruby-dev:48323] [Bug #9977]'
assert_no_memory_leak([], <<-'end;', "5_000.times {ENV[k] = v}", bug9977)
ENV.clear
k = 'FOO'
v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500)
end;
end
end end