[前][次][番号順一覧][スレッド一覧]

ruby-changes:52085

From: nobu <ko1@a...>
Date: Sat, 11 Aug 2018 22:19:01 +0900 (JST)
Subject: [ruby-changes:52085] nobu:r64293 (trunk): hash.c: env block size limit on Windows

nobu	2018-08-11 22:18:55 +0900 (Sat, 11 Aug 2018)

  New Revision: 64293

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64293

  Log:
    hash.c: env block size limit on Windows
    
    * hash.c (ruby_setenv): do not check environment block size.
      c.f. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx
      Starting with Windows Vista and Windows Server 2008, there is no
      technical limitation on the size of the environment block.
      [ruby-core:88400] [Bug #14979]

  Modified files:
    trunk/hash.c
    trunk/test/ruby/test_env.rb
Index: test/ruby/test_env.rb
===================================================================
--- test/ruby/test_env.rb	(revision 64292)
+++ test/ruby/test_env.rb	(revision 64293)
@@ -457,7 +457,7 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L457
   def test_huge_value
     huge_value = "bar" * 40960
     ENV["foo"] = "bar"
-    if /mswin|mingw/ =~ RUBY_PLATFORM
+    if /mswin|mingw/ =~ RUBY_PLATFORM && windows_version < 6
       assert_raise(Errno::EINVAL) { ENV["foo"] = huge_value }
       assert_equal("bar", ENV["foo"])
     else
@@ -467,6 +467,10 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L467
   end
 
   if /mswin|mingw/ =~ RUBY_PLATFORM
+    def windows_version
+      @windows_version ||= %x[ver][/Version (\d+)/, 1].to_i
+    end
+
     def test_win32_blocksize
       keys = []
       len = 32767 - ENV.to_a.flatten.inject(1) {|r,e| r + e.bytesize + 1}
@@ -476,9 +480,15 @@ class TestEnv < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_env.rb#L480
         keys << key
         ENV[key] = val
       end
-      1.upto(12) {|i|
-        assert_raise(Errno::EINVAL) { ENV[key] = val }
-      }
+      if windows_version < 6
+        1.upto(12) {|i|
+          assert_raise(Errno::EINVAL) { ENV[key] = val }
+        }
+      else
+        1.upto(12) {|i|
+          assert_nothing_raised(Errno::EINVAL) { ENV[key] = val }
+        }
+      end
     ensure
       keys.each {|k| ENV.delete(k)}
     end
Index: hash.c
===================================================================
--- hash.c	(revision 64292)
+++ hash.c	(revision 64293)
@@ -3549,10 +3549,33 @@ getenvsize(const WCHAR* p) https://github.com/ruby/ruby/blob/trunk/hash.c#L3549
     while (*p++) p += lstrlenW(p) + 1;
     return p - porg + 1;
 }
+
 static size_t
 getenvblocksize(void)
 {
+#ifdef _MAX_ENV
+    return _MAX_ENV;
+#else
     return 32767;
+#endif
+}
+
+static int
+check_envsize(size_t n)
+{
+    if (_WIN32_WINNT < 0x0600 && rb_w32_osver() < 6) {
+	/* https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx */
+	/* Windows Server 2003 and Windows XP: The maximum size of the
+	 * environment block for the process is 32,767 characters. */
+	WCHAR* p = GetEnvironmentStringsW();
+	if (!p) return -1; /* never happen */
+	n += getenvsize(p);
+	FreeEnvironmentStringsW(p);
+	if (n >= getenvblocksize()) {
+	    return -1;
+	}
+    }
+    return 0;
 }
 #endif
 
@@ -3592,16 +3615,11 @@ ruby_setenv(const char *name, const char https://github.com/ruby/ruby/blob/trunk/hash.c#L3615
     check_envname(name);
     len = MultiByteToWideChar(CP_UTF8, 0, name, -1, NULL, 0);
     if (value) {
-	WCHAR* p = GetEnvironmentStringsW();
-	size_t n;
 	int len2;
-	if (!p) goto fail; /* never happen */
-	n = lstrlen(name) + 2 + strlen(value) + getenvsize(p);
-	FreeEnvironmentStringsW(p);
-	if (n >= getenvblocksize()) {
+	len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
+	if (check_envsize((size_t)len + len2)) { /* len and len2 include '\0' */
 	    goto fail;  /* 2 for '=' & '\0' */
 	}
-	len2 = MultiByteToWideChar(CP_UTF8, 0, value, -1, NULL, 0);
 	wname = ALLOCV_N(WCHAR, buf, len + len2);
 	wvalue = wname + len;
 	MultiByteToWideChar(CP_UTF8, 0, name, -1, wname, len);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]