From da47bbd1db646794fba94449d4d226d35ec2b2ff Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 11 Sep 2010 07:47:44 +0000 Subject: * hash.c (ruby_setenv): raise if putenv and SetEnvironmentVariable failed, because of the restriction of the size on Windows. based on a patch from Peter Weldon at [ruby-core:32304]. fix: Bug#3812, [ruby-core:32250] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- hash.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 8bba586dfb..73f90125f9 100644 --- a/hash.c +++ b/hash.c @@ -2139,29 +2139,28 @@ void ruby_setenv(const char *name, const char *value) { #if defined(_WIN32) - int len; - char *buf; + VALUE buf; + int failed = 0; if (strchr(name, '=')) { + fail: errno = EINVAL; rb_sys_fail("ruby_setenv"); } 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); + } + failed = putenv(RSTRING_PTR(buf)); + /* even if putenv() failed, clean up and try to delete the + * variable from the system area. */ + rb_str_resize(buf, 0); + if (!value || !*value) { + /* putenv() doesn't handle empty value */ + if (!SetEnvironmentVariable(name,value)) goto fail; } + if (failed) goto fail; #elif defined(HAVE_SETENV) && defined(HAVE_UNSETENV) #undef setenv #undef unsetenv -- cgit v1.2.3