From 5281610f919f96fd0ec1a14a53ad42b54bc8d373 Mon Sep 17 00:00:00 2001 From: arton Date: Sat, 19 Mar 2011 09:28:49 +0000 Subject: [PATCH] * hash.c (ruby_setenv): calculate total env block size for win32. * test/ruby/test_env.rb: add test for above patch. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31130 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ hash.c | 16 +++++++++++++++- test/ruby/test_env.rb | 12 ++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index af066ae012..89ff91f273 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Mar 19 18:35:05 2011 Tajima Akio + + * hash.c (ruby_setenv): calculate total env block size for win32. + * test/ruby/test_env.rb: add test for above patch. + Sat Mar 19 17:14:46 2011 Tajima Akio * hash.c (ruby_setenv): checking with max process environment diff --git a/hash.c b/hash.c index b303211db7..e847b4dd3a 100644 --- a/hash.c +++ b/hash.c @@ -2194,6 +2194,20 @@ envix(const char *nam) } #endif +#if defined(_WIN32) +static int +getenvsize(char* p) +{ + char prev = *p++; + int len = 1; + for (; prev || *p; p++) { + prev = *p; + len++; + } + return len; +} +#endif + void ruby_setenv(const char *name, const char *value) { @@ -2208,7 +2222,7 @@ ruby_setenv(const char *name, const char *value) if (value) { char* p = GetEnvironmentStringsA(); if (p) { - if (strlen(value) + strlen(p) >= 32767) goto fail; + if (strlen(name) + 1 + strlen(value) + getenvsize(p) >= 32767) goto fail; } else { if (strlen(value) >= 5120) goto fail; } diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index 681b21d614..b987a4bac0 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -387,4 +387,16 @@ class TestEnv < Test::Unit::TestCase assert_equal(huge_value, ENV["foo"]) end end + + if /mswin|mingw/ =~ RUBY_PLATFORM + def test_win32_blocksize + len = 32767 - ENV.to_a.flatten.inject(0) {|r,e| r + e.size + 2 } + val = "bar" * 1000 + key = nil + 1.upto(12) {|i| + ENV[key] = val while (len -= val.size + (key="foo#{len}").size + 2) > 0 + assert_raise(Errno::EINVAL) { ENV[key] = val } + } + end + end end