summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-11 13:18:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-11 13:18:55 +0000
commit490fbc4c858e1b37f338c59366a84cc228c53c3e (patch)
tree773d6580d52dace6d6f2713eeee2b5e84726d395 /hash.c
parent3d2e7b615869dd539b7b250b8d14007d82c2d15d (diff)
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c32
1 files changed, 25 insertions, 7 deletions
diff --git a/hash.c b/hash.c
index c457923..d58e67c 100644
--- a/hash.c
+++ b/hash.c
@@ -3549,10 +3549,33 @@ getenvsize(const WCHAR* p)
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 *value)
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);