diff --git a/ChangeLog b/ChangeLog index 2d25269ac7..07d846dc58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Thu Jul 10 23:51:36 2014 Naohisa Goto + + * hash.c (ruby_setenv): Fix TestEnv#test_aset failure on Solaris 9. + When name contains '=', ruby_setenv raises Errno::EINVAL. + That is the same behavior as Solaris 10. + NULL check for malloc return value is also added. + Thu Jul 10 15:02:55 2014 Nobuyoshi Nakada * vm_insnhelper.c (vm_callee_setup_keyword_arg): adjust VM stack diff --git a/hash.c b/hash.c index 4bf0ae3715..c600fd11ab 100644 --- a/hash.c +++ b/hash.c @@ -2729,6 +2729,10 @@ getenvblocksize() { return (rb_w32_osver() >= 5) ? 32767 : 5120; } +#endif + +#if defined(_WIN32) || \ + (defined(__sun) && !(defined(HAVE_SETENV) && defined(HAVE_UNSETENV))) NORETURN(static void invalid_envname(const char *name)); @@ -2798,10 +2802,22 @@ ruby_setenv(const char *name, const char *value) #endif } #elif defined __sun - size_t len; - char **env_ptr, *str; + /* Solaris 9 (or earlier) does not have setenv(3C) and unsetenv(3C). */ + /* The below code was tested on Solaris 10 by: + % ./configure ac_cv_func_setenv=no ac_cv_func_unsetenv=no + */ + size_t len, mem_size; + char **env_ptr, *str, *mem_ptr; + check_envname(name); len = strlen(name); + if (value) { + mem_size = len + strlen(value) + 2; + mem_ptr = malloc(mem_size); + if (mem_ptr == NULL) + rb_sys_fail_str(rb_sprintf("malloc("PRIuSIZE")", mem_size)); + snprintf(mem_ptr, mem_size, "%s=%s", name, value); + } for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) { if (!strncmp(str, name, len) && str[len] == '=') { if (!in_origenv(str)) free(str); @@ -2810,10 +2826,10 @@ ruby_setenv(const char *name, const char *value) } } if (value) { - str = malloc(len += strlen(value) + 2); - snprintf(str, len, "%s=%s", name, value); - if (putenv(str)) + if (putenv(mem_ptr)) { + free(mem_ptr); rb_sys_fail_str(rb_sprintf("putenv(%s)", name)); + } } #else /* WIN32 */ size_t len;