src/os/solaris/vm/os_solaris.cpp

Print this page
rev 6136 : 8038201: Clean up misleading usage of malloc() in init_system_properties_values()
Summary: Add missing freeing of memory or use local variable. Also add a 'const' to avoid gcc write-string warnings on ppc.

*** 669,731 **** // Otherwise exit. // // Important note: if the location of libjvm.so changes this // code needs to be changed accordingly. - // The next few definitions allow the code to be verbatim: - #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n), mtInternal) - #define free(p) FREE_C_HEAP_ARRAY(char, p, mtInternal) - #define getenv(n) ::getenv(n) - #define EXTENSIONS_DIR "/lib/ext" #define ENDORSED_DIR "/lib/endorsed" #define COMMON_DIR "/usr/jdk/packages" { - /* sysclasspath, java_home, dll_dir */ - { - char *home_path; - char *dll_path; char *pslash; char buf[MAXPATHLEN]; os::jvm_path(buf, sizeof(buf)); // Found the full path to libjvm.so. // Now cut the path to <java_home>/jre if we can. ! *(strrchr(buf, '/')) = '\0'; /* get rid of /libjvm.so */ pslash = strrchr(buf, '/'); ! if (pslash != NULL) ! *pslash = '\0'; /* get rid of /{client|server|hotspot} */ ! dll_path = malloc(strlen(buf) + 1); ! if (dll_path == NULL) ! return; ! strcpy(dll_path, buf); ! Arguments::set_dll_dir(dll_path); if (pslash != NULL) { pslash = strrchr(buf, '/'); if (pslash != NULL) { ! *pslash = '\0'; /* get rid of /<arch> */ pslash = strrchr(buf, '/'); ! if (pslash != NULL) ! *pslash = '\0'; /* get rid of /lib */ } } ! home_path = malloc(strlen(buf) + 1); ! if (home_path == NULL) ! return; ! strcpy(home_path, buf); ! Arguments::set_java_home(home_path); ! ! if (!set_boot_path('/', ':')) return; } ! /* ! * Where to look for native libraries ! */ { // Use dlinfo() to determine the correct java.library.path. // // If we're launched by the Java launcher, and the user // does not set java.library.path explicitly on the commandline, --- 669,715 ---- // Otherwise exit. // // Important note: if the location of libjvm.so changes this // code needs to be changed accordingly. #define EXTENSIONS_DIR "/lib/ext" #define ENDORSED_DIR "/lib/endorsed" #define COMMON_DIR "/usr/jdk/packages" + // sysclasspath, java_home, dll_dir { char *pslash; char buf[MAXPATHLEN]; os::jvm_path(buf, sizeof(buf)); // Found the full path to libjvm.so. // Now cut the path to <java_home>/jre if we can. ! *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so. pslash = strrchr(buf, '/'); ! if (pslash != NULL) { ! *pslash = '\0'; // Get rid of /{client|server|hotspot}. ! } ! Arguments::set_dll_dir(buf); if (pslash != NULL) { pslash = strrchr(buf, '/'); if (pslash != NULL) { ! *pslash = '\0'; // Get rid of /<arch>. pslash = strrchr(buf, '/'); ! if (pslash != NULL) { ! *pslash = '\0'; // Get rid of /lib. } } + } + Arguments::set_java_home(buf); ! if (!set_boot_path('/', ':')) { return; } + } ! // Where to look for native libraries. { // Use dlinfo() to determine the correct java.library.path. // // If we're launched by the Java launcher, and the user // does not set java.library.path explicitly on the commandline,
*** 745,771 **** Dl_serpath *path; char* library_path; char *common_path; int i; ! // determine search path count and required buffer size if (dlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info) == -1) { vm_exit_during_initialization("dlinfo SERINFOSIZE request", dlerror()); } ! // allocate new buffer and initialize ! info = (Dl_serinfo*)malloc(_info.dls_size); ! if (info == NULL) { ! vm_exit_out_of_memory(_info.dls_size, OOM_MALLOC_ERROR, ! "init_system_properties_values info"); ! } info->dls_size = _info.dls_size; info->dls_cnt = _info.dls_cnt; ! // obtain search path information if (dlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info) == -1) { ! free(info); vm_exit_during_initialization("dlinfo SERINFO request", dlerror()); } path = &info->dls_serpath[0]; --- 729,751 ---- Dl_serpath *path; char* library_path; char *common_path; int i; ! // Determine search path count and required buffer size. if (dlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info) == -1) { vm_exit_during_initialization("dlinfo SERINFOSIZE request", dlerror()); } ! // Allocate new buffer and initialize. ! info = (Dl_serinfo*)NEW_C_HEAP_ARRAY(char, _info.dls_size, mtInternal); info->dls_size = _info.dls_size; info->dls_cnt = _info.dls_cnt; ! // Obtain search path information. if (dlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info) == -1) { ! FREE_C_HEAP_ARRAY(char, info, mtInternal); vm_exit_during_initialization("dlinfo SERINFO request", dlerror()); } path = &info->dls_serpath[0];
*** 782,830 **** sysinfo(SI_ARCHITECTURE, cpu_arch, sizeof(cpu_arch)); #ifdef _LP64 // If we are a 64-bit vm, perform the following translations: // sparc -> sparcv9 // i386 -> amd64 ! if (strcmp(cpu_arch, "sparc") == 0) strcat(cpu_arch, "v9"); ! else if (strcmp(cpu_arch, "i386") == 0) strcpy(cpu_arch, "amd64"); #endif // Construct the invariant part of ld_library_path. Note that the // space for the colon and the trailing null are provided by the // nulls included by the sizeof operator. size_t bufsize = sizeof(COMMON_DIR) + sizeof("/lib/") + strlen(cpu_arch); ! common_path = malloc(bufsize); ! if (common_path == NULL) { ! free(info); ! vm_exit_out_of_memory(bufsize, OOM_MALLOC_ERROR, ! "init_system_properties_values common_path"); ! } sprintf(common_path, COMMON_DIR "/lib/%s", cpu_arch); // struct size is more than sufficient for the path components obtained // through the dlinfo() call, so only add additional space for the path // components explicitly added here. bufsize = info->dls_size + strlen(common_path); ! library_path = malloc(bufsize); ! if (library_path == NULL) { ! free(info); ! free(common_path); ! vm_exit_out_of_memory(bufsize, OOM_MALLOC_ERROR, ! "init_system_properties_values library_path"); ! } library_path[0] = '\0'; // Construct the desired Java library path from the linker's library // search path. // // For compatibility, it is optimal that we insert the additional path // components specific to the Java VM after those components specified // in LD_LIBRARY_PATH (if any) but before those added by the ld.so // infrastructure. ! if (info->dls_cnt == 0) { // Not sure this can happen, but allow for it strcpy(library_path, common_path); } else { int inserted = 0; for (i = 0; i < info->dls_cnt; i++, path++) { uint_t flags = path->dls_flags & LA_SER_MASK; --- 762,800 ---- sysinfo(SI_ARCHITECTURE, cpu_arch, sizeof(cpu_arch)); #ifdef _LP64 // If we are a 64-bit vm, perform the following translations: // sparc -> sparcv9 // i386 -> amd64 ! if (strcmp(cpu_arch, "sparc") == 0) { strcat(cpu_arch, "v9"); ! } else if (strcmp(cpu_arch, "i386") == 0) { strcpy(cpu_arch, "amd64"); + } #endif // Construct the invariant part of ld_library_path. Note that the // space for the colon and the trailing null are provided by the // nulls included by the sizeof operator. size_t bufsize = sizeof(COMMON_DIR) + sizeof("/lib/") + strlen(cpu_arch); ! common_path = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); sprintf(common_path, COMMON_DIR "/lib/%s", cpu_arch); // struct size is more than sufficient for the path components obtained // through the dlinfo() call, so only add additional space for the path // components explicitly added here. bufsize = info->dls_size + strlen(common_path); ! library_path = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); library_path[0] = '\0'; // Construct the desired Java library path from the linker's library // search path. // // For compatibility, it is optimal that we insert the additional path // components specific to the Java VM after those components specified // in LD_LIBRARY_PATH (if any) but before those added by the ld.so // infrastructure. ! if (info->dls_cnt == 0) { // Not sure this can happen, but allow for it. strcpy(library_path, common_path); } else { int inserted = 0; for (i = 0; i < info->dls_cnt; i++, path++) { uint_t flags = path->dls_flags & LA_SER_MASK;
*** 834,890 **** inserted = 1; } strcat(library_path, path->dls_name); strcat(library_path, os::path_separator()); } ! // eliminate trailing path separator library_path[strlen(library_path)-1] = '\0'; } // happens before argument parsing - can't use a trace flag // tty->print_raw("init_system_properties_values: native lib path: "); // tty->print_raw_cr(library_path); // callee copies into its own buffer Arguments::set_library_path(library_path); ! free(common_path); ! free(library_path); ! free(info); } ! /* ! * Extensions directories. ! * ! * Note that the space for the colon and the trailing null are provided ! * by the nulls included by the sizeof operator (so actually one byte more ! * than necessary is allocated). ! */ ! { ! char *buf = (char *) malloc(strlen(Arguments::get_java_home()) + ! sizeof(EXTENSIONS_DIR) + sizeof(COMMON_DIR) + ! sizeof(EXTENSIONS_DIR)); ! sprintf(buf, "%s" EXTENSIONS_DIR ":" COMMON_DIR EXTENSIONS_DIR, ! Arguments::get_java_home()); Arguments::set_ext_dirs(buf); - } ! /* Endorsed standards default directory. */ ! { ! char * buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR)); sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home()); Arguments::set_endorsed_dirs(buf); - } - } - #undef malloc - #undef free - #undef getenv #undef EXTENSIONS_DIR #undef ENDORSED_DIR #undef COMMON_DIR - } void os::breakpoint() { BREAKPOINT; } --- 804,846 ---- inserted = 1; } strcat(library_path, path->dls_name); strcat(library_path, os::path_separator()); } ! // Eliminate trailing path separator. library_path[strlen(library_path)-1] = '\0'; } // happens before argument parsing - can't use a trace flag // tty->print_raw("init_system_properties_values: native lib path: "); // tty->print_raw_cr(library_path); // callee copies into its own buffer Arguments::set_library_path(library_path); ! FREE_C_HEAP_ARRAY(char, common_path, mtInternal); ! FREE_C_HEAP_ARRAY(char, library_path, mtInternal); ! FREE_C_HEAP_ARRAY(char, info, mtInternal); } ! // Buffer that fits both sprintfs. ! // Note that the space for the colon and the trailing null are provided ! // by the nulls included by the sizeof operator (so actually one byte more ! // than necessary is allocated). ! char buf[MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(COMMON_DIR) + sizeof(EXTENSIONS_DIR) + sizeof(ENDORSED_DIR)]; ! ! // Extensions directories. ! sprintf(buf, "%s" EXTENSIONS_DIR ":" COMMON_DIR EXTENSIONS_DIR, Arguments::get_java_home()); Arguments::set_ext_dirs(buf); ! // Endorsed standards default directory. sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home()); Arguments::set_endorsed_dirs(buf); #undef EXTENSIONS_DIR #undef ENDORSED_DIR #undef COMMON_DIR } void os::breakpoint() { BREAKPOINT; }