Hi,

At Fri, 10 Sep 2010 02:52:07 +0900,
Peter Weldon wrote in [ruby-core:32250]:
> On mswin&mingw the size of an environment key, value pair is
> bounded by _MAX_ENV. Calls to putenv() or _putenv() that
> exceed this will fail and not set the value.

Thank you, but alloca is not safe for such huge values.  IIRC,
it could return invalid address silently.


diff --git a/hash.c b/hash.c index 8bba586..638f493 100644 --- a/hash.c +++ b/hash.c @@ -2139,28 +2139,24 @@ void ruby_setenv(const char *name, const char *value) { #if defined(_WIN32) - int len; - char *buf; + VALUE buf; if (strchr(name, '=')) { + fail: errno = EINVAL; rb_sys_fail("ruby_setenv"); } + len = strlen(name) + 1 + 1; if (value) { - len = strlen(name) + 1 + strlen(value) + 1; - buf = ALLOCA_N(char, len); - snprintf(buf, len, "%s=%s", name, value); - putenv(buf); - - /* putenv() doesn't handle empty value */ - if (!*value) - SetEnvironmentVariable(name,value); + buf = rb_sprintf("%s=%s", name, value); } else { - len = strlen(name) + 1 + 1; - buf = ALLOCA_N(char, len); - snprintf(buf, len, "%s=", name); - putenv(buf); - SetEnvironmentVariable(name, 0); + buf = rb_sprintf("%s=", name); + } + if (putenv(RSTRING_PTR(buf))) goto fail; + rb_str_resize(buf); + if (!value || !*value) { + /* putenv() doesn't handle empty value */ + if (SetEnvironmentVariable(name,value)) goto fail; } #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV) #undef setenv
-- Nobu Nakada