Skip to content

Commit 6508fff

Browse files
committed
merge revision(s) r46550: [Backport ruby#9977]
* 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/branches/ruby_2_1@47053 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 73a3fc3 commit 6508fff

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Mon Aug 4 01:24:09 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
Mon Aug 4 01:11:07 2014 Nobuyoshi Nakada <[email protected]>
28

39
* re.c (match_aref, rb_reg_regsub): consider encoding of captured

hash.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,9 +2751,12 @@ ruby_setenv(const char *name, const char *value)
27512751
int failed = 0;
27522752
check_envname(name);
27532753
if (value) {
2754-
const char* p = GetEnvironmentStringsA();
2754+
char* p = GetEnvironmentStringsA();
2755+
size_t n;
27552756
if (!p) goto fail; /* never happen */
2756-
if (strlen(name) + 2 + strlen(value) + getenvsize(p) >= getenvblocksize()) {
2757+
n = strlen(name) + 2 + strlen(value) + getenvsize(p);
2758+
FreeEnvironmentStringsA(p);
2759+
if (n >= getenvblocksize()) {
27572760
goto fail; /* 2 for '=' & '\0' */
27582761
}
27592762
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
@@ -408,4 +409,13 @@ def test_win32_blocksize
408409
keys.each {|k| ENV.delete(k)}
409410
end
410411
end
412+
413+
def test_memory_leak_aset
414+
bug9977 = '[ruby-dev:48323] [Bug #9977]'
415+
assert_no_memory_leak([], <<-'end;', "5_000.times {ENV[k] = v}", bug9977)
416+
ENV.clear
417+
k = 'FOO'
418+
v = (ENV[k] = 'bar'*5000 rescue 'bar'*1500)
419+
end;
420+
end
411421
end

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.2"
22
#define RUBY_RELEASE_DATE "2014-08-04"
3-
#define RUBY_PATCHLEVEL 191
3+
#define RUBY_PATCHLEVEL 192
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 8

0 commit comments

Comments
 (0)