summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-11 07:47:44 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-09-11 07:47:44 +0000
commitda47bbd1db646794fba94449d4d226d35ec2b2ff (patch)
tree4438039d7593722c3312dbe5bbf347d6b6d82a08 /hash.c
parent073e6ccc7ba29e1391865107f105b8069352ee52 (diff)
* 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
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c29
1 files changed, 14 insertions, 15 deletions
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