--- old/src/os/aix/vm/os_aix.cpp 2015-03-13 09:58:48.498512000 -0700 +++ new/src/os/aix/vm/os_aix.cpp 2015-03-13 09:58:48.343514000 -0700 @@ -257,19 +257,6 @@ return Aix::physical_memory(); } -//////////////////////////////////////////////////////////////////////////////// -// environment support - -bool os::getenv(const char* name, char* buf, int len) { - const char* val = ::getenv(name); - if (val != NULL && strlen(val) < (size_t)len) { - strcpy(buf, val); - return true; - } - if (len > 0) buf[0] = 0; // return a null string - return false; -} - // Return true if user is running as root. bool os::have_special_privileges() { --- old/src/os/bsd/vm/os_bsd.cpp 2015-03-13 09:58:49.033532000 -0700 +++ new/src/os/bsd/vm/os_bsd.cpp 2015-03-13 09:58:48.943519000 -0700 @@ -190,20 +190,6 @@ return Bsd::physical_memory(); } -//////////////////////////////////////////////////////////////////////////////// -// environment support - -bool os::getenv(const char* name, char* buf, int len) { - const char* val = ::getenv(name); - if (val != NULL && strlen(val) < (size_t)len) { - strcpy(buf, val); - return true; - } - if (len > 0) buf[0] = 0; // return a null string - return false; -} - - // Return true if user is running as root. bool os::have_special_privileges() { --- old/src/os/linux/vm/os_linux.cpp 2015-03-13 09:58:49.567525000 -0700 +++ new/src/os/linux/vm/os_linux.cpp 2015-03-13 09:58:49.471518000 -0700 @@ -184,20 +184,6 @@ return Linux::physical_memory(); } -//////////////////////////////////////////////////////////////////////////////// -// environment support - -bool os::getenv(const char* name, char* buf, int len) { - const char* val = ::getenv(name); - if (val != NULL && strlen(val) < (size_t)len) { - strcpy(buf, val); - return true; - } - if (len > 0) buf[0] = 0; // return a null string - return false; -} - - // Return true if user is running as root. bool os::have_special_privileges() { --- old/src/os/solaris/vm/os_solaris.cpp 2015-03-13 09:58:50.169543000 -0700 +++ new/src/os/solaris/vm/os_solaris.cpp 2015-03-13 09:58:50.068530000 -0700 @@ -555,17 +555,6 @@ return (bind_result == 0); } -bool os::getenv(const char* name, char* buffer, int len) { - char* val = ::getenv(name); - if (val == NULL || strlen(val) + 1 > len) { - if (len > 0) buffer[0] = 0; // return a null string - return false; - } - strcpy(buffer, val); - return true; -} - - // Return true if user is running as root. bool os::have_special_privileges() { --- old/src/os/windows/vm/os_windows.cpp 2015-03-13 09:58:50.752531000 -0700 +++ new/src/os/windows/vm/os_windows.cpp 2015-03-13 09:58:50.664523000 -0700 @@ -153,11 +153,6 @@ // Implementation of os -bool os::getenv(const char* name, char* buffer, int len) { - int result = GetEnvironmentVariable(name, buffer, len); - return result > 0 && result < len; -} - bool os::unsetenv(const char* name) { assert(name != NULL, "Null pointer"); return (SetEnvironmentVariable(name, NULL) == TRUE); @@ -5930,4 +5925,3 @@ UseNUMAInterleaving = old_use_numa_interleaving; } #endif // PRODUCT - --- old/src/share/vm/runtime/arguments.cpp 2015-03-13 09:58:51.343528000 -0700 +++ new/src/share/vm/runtime/arguments.cpp 2015-03-13 09:58:51.226528000 -0700 @@ -3511,15 +3511,16 @@ if (os::is_headless_jre()) { const char* headless = Arguments::get_property("java.awt.headless"); if (headless == NULL) { - char envbuffer[128]; - if (!os::getenv("JAVA_AWT_HEADLESS", envbuffer, sizeof(envbuffer))) { + const char *headless_env = ::getenv("JAVA_AWT_HEADLESS"); + if (headless_env == NULL) { if (!add_property("java.awt.headless=true")) { return JNI_ENOMEM; } } else { char buffer[256]; - strcpy(buffer, "java.awt.headless="); - strcat(buffer, envbuffer); + const char *key = "java.awt.headless="; + strcpy(buffer, key); + strncat(buffer, headless_env, 256 - strlen(key) - 1); if (!add_property(buffer)) { return JNI_ENOMEM; } @@ -3550,75 +3551,90 @@ } jint Arguments::parse_options_environment_variable(const char* name, SysClassPath* scp_p, bool* scp_assembly_required_p) { - const int N_MAX_OPTIONS = 64; - const int OPTION_BUFFER_SIZE = 1024; - char buffer[OPTION_BUFFER_SIZE]; + char *buffer = ::getenv(name); - // The variable will be ignored if it exceeds the length of the buffer. // Don't check this variable if user has special privileges // (e.g. unix su command). - if (os::getenv(name, buffer, sizeof(buffer)) && - !os::have_special_privileges()) { - JavaVMOption options[N_MAX_OPTIONS]; // Construct option array - jio_fprintf(defaultStream::error_stream(), - "Picked up %s: %s\n", name, buffer); - char* rd = buffer; // pointer to the input string (rd) - int i; - for (i = 0; i < N_MAX_OPTIONS;) { // repeat for all options in the input string - while (isspace(*rd)) rd++; // skip whitespace - if (*rd == 0) break; // we re done when the input string is read completely - - // The output, option string, overwrites the input string. - // Because of quoting, the pointer to the option string (wrt) may lag the pointer to - // input string (rd). - char* wrt = rd; - - options[i++].optionString = wrt; // Fill in option - while (*rd != 0 && !isspace(*rd)) { // unquoted strings terminate with a space or NULL - if (*rd == '\'' || *rd == '"') { // handle a quoted string - int quote = *rd; // matching quote to look for - rd++; // don't copy open quote - while (*rd != quote) { // include everything (even spaces) up until quote - if (*rd == 0) { // string termination means unmatched string - jio_fprintf(defaultStream::error_stream(), - "Unmatched quote in %s\n", name); - return JNI_ERR; - } - *wrt++ = *rd++; // copy to option string + if (buffer == NULL || os::have_special_privileges()) { + return JNI_OK; + } + + if ((buffer = os::strdup(buffer)) == NULL) { + return JNI_ENOMEM; + } + GrowableArray options(2, true); // Construct option array + jio_fprintf(defaultStream::error_stream(), + "Picked up %s: %s\n", name, buffer); + char* rd = buffer; // pointer to the input string (rd) + while (true) { // repeat for all options in the input string + while (isspace(*rd)) rd++; // skip whitespace + if (*rd == 0) break; // we re done when the input string is read completely + + // The output, option string, overwrites the input string. + // Because of quoting, the pointer to the option string (wrt) may lag the pointer to + // input string (rd). + char* wrt = rd; + + JavaVMOption option; + option.optionString = wrt; + options.append(option); // Fill in option + while (*rd != 0 && !isspace(*rd)) { // unquoted strings terminate with a space or NULL + if (*rd == '\'' || *rd == '"') { // handle a quoted string + int quote = *rd; // matching quote to look for + rd++; // don't copy open quote + while (*rd != quote) { // include everything (even spaces) up until quote + if (*rd == 0) { // string termination means unmatched string + jio_fprintf(defaultStream::error_stream(), + "Unmatched quote in %s\n", name); + os::free(buffer); + return JNI_ERR; } - rd++; // don't copy close quote - } else { - *wrt++ = *rd++; // copy to option string + *wrt++ = *rd++; // copy to option string } + rd++; // don't copy close quote + } else { + *wrt++ = *rd++; // copy to option string } - // Need to check if we're done before writing a NULL, - // because the write could be to the byte that rd is pointing to. - if (*rd++ == 0) { - *wrt = 0; - break; - } - *wrt = 0; // Zero terminate option } - // Construct JavaVMInitArgs structure and parse as if it was part of the command line - JavaVMInitArgs vm_args; - vm_args.version = JNI_VERSION_1_2; - vm_args.options = options; - vm_args.nOptions = i; - vm_args.ignoreUnrecognized = IgnoreUnrecognizedVMOptions; - - if (PrintVMOptions) { - const char* tail; - for (int i = 0; i < vm_args.nOptions; i++) { - const JavaVMOption *option = vm_args.options + i; - if (match_option(option, "-XX:", &tail)) { - logOption(tail); - } - } + // Need to check if we're done before writing a NULL, + // because the write could be to the byte that rd is pointing to. + if (*rd++ == 0) { + *wrt = 0; + break; } + *wrt = 0; // Zero terminate option + } + JavaVMOption* options_arr = + NEW_C_HEAP_ARRAY_RETURN_NULL(JavaVMOption, options.length(), mtInternal); + if (options_arr == NULL) { + return JNI_ENOMEM; + } + for (int i = 0; i < options.length(); i++) { + options_arr[i] = options.at(i); + } - return(parse_each_vm_init_arg(&vm_args, scp_p, scp_assembly_required_p, Flag::ENVIRON_VAR)); + // Construct JavaVMInitArgs structure and parse as if it was part of the command line + JavaVMInitArgs vm_args; + vm_args.version = JNI_VERSION_1_2; + vm_args.options = options_arr; + vm_args.nOptions = options.length(); + vm_args.ignoreUnrecognized = IgnoreUnrecognizedVMOptions; + + if (PrintVMOptions) { + const char* tail; + for (int i = 0; i < vm_args.nOptions; i++) { + const JavaVMOption *option = vm_args.options + i; + if (match_option(option, "-XX:", &tail)) { + logOption(tail); + } + } } - return JNI_OK; + + jint result = parse_each_vm_init_arg(&vm_args, scp_p, scp_assembly_required_p, + Flag::ENVIRON_VAR); + FREE_C_HEAP_ARRAY(JavaVMOption, options_arr); + os::free(buffer); + return result; } void Arguments::set_shared_spaces_flags() { --- old/src/share/vm/runtime/os.cpp 2015-03-13 09:58:51.875534000 -0700 +++ new/src/share/vm/runtime/os.cpp 2015-03-13 09:58:51.791531000 -0700 @@ -813,16 +813,16 @@ st->cr(); } -void os::print_environment_variables(outputStream* st, const char** env_list, - char* buffer, int len) { +void os::print_environment_variables(outputStream* st, const char** env_list) { if (env_list) { st->print_cr("Environment Variables:"); for (int i = 0; env_list[i] != NULL; i++) { - if (getenv(env_list[i], buffer, len)) { + char *envvar = ::getenv(env_list[i]); + if (envvar != NULL) { st->print("%s", env_list[i]); st->print("="); - st->print_cr("%s", buffer); + st->print_cr("%s", envvar); } } } --- old/src/share/vm/runtime/os.hpp 2015-03-13 09:58:52.337534000 -0700 +++ new/src/share/vm/runtime/os.hpp 2015-03-13 09:58:52.254535000 -0700 @@ -164,8 +164,7 @@ // Override me as needed static int file_name_strcmp(const char* s1, const char* s2); - // get/unset environment variable - static bool getenv(const char* name, char* buffer, int len); + // unset environment variable static bool unsetenv(const char* name); static bool have_special_privileges(); @@ -591,7 +590,7 @@ static void pd_print_cpu_info(outputStream* st); static void print_memory_info(outputStream* st); static void print_dll_info(outputStream* st); - static void print_environment_variables(outputStream* st, const char** env_list, char* buffer, int len); + static void print_environment_variables(outputStream* st, const char** env_list); static void print_context(outputStream* st, void* context); static void print_register_info(outputStream* st, void* context); static void print_siginfo(outputStream* st, void* siginfo); --- old/src/share/vm/services/memTracker.cpp 2015-03-13 09:58:52.853534000 -0700 +++ new/src/share/vm/services/memTracker.cpp 2015-03-13 09:58:52.773536000 -0700 @@ -47,9 +47,9 @@ NMT_TrackingLevel MemTracker::init_tracking_level() { NMT_TrackingLevel level = NMT_off; char buf[64]; - char nmt_option[64]; jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id()); - if (os::getenv(buf, nmt_option, sizeof(nmt_option))) { + const char *nmt_option = ::getenv(buf); + if (nmt_option != NULL) { if (strcmp(nmt_option, "summary") == 0) { level = NMT_summary; } else if (strcmp(nmt_option, "detail") == 0) { @@ -323,4 +323,3 @@ out->print_cr(" "); walker.report_statistics(out); } - --- old/src/share/vm/utilities/vmError.cpp 2015-03-13 09:58:53.323542000 -0700 +++ new/src/share/vm/utilities/vmError.cpp 2015-03-13 09:58:53.236541000 -0700 @@ -748,7 +748,7 @@ STEP(220, "(printing environment variables)" ) if (_verbose) { - os::print_environment_variables(st, env_list, buf, sizeof(buf)); + os::print_environment_variables(st, env_list); st->cr(); }