src/os/bsd/vm/os_bsd.cpp

Print this page
rev 6140 : 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.

@@ -304,13 +304,10 @@
   return home_dir;
 }
 #endif
 
 void os::init_system_properties_values() {
-//  char arch[12];
-//  sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
-
   // The next steps are taken in the product version:
   //
   // Obtain the JAVA_HOME value from the location of libjvm.so.
   // This library should be located at:
   // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.

@@ -333,141 +330,195 @@
   // 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 getenv(n) ::getenv(n)
-
-/*
- * See ld(1):
- *      The linker uses the following search paths to locate required
- *      shared libraries:
- *        1: ...
- *        ...
- *        7: The default directories, normally /lib and /usr/lib.
- */
+// See ld(1):
+//      The linker uses the following search paths to locate required
+//      shared libraries:
+//        1: ...
+//        ...
+//        7: The default directories, normally /lib and /usr/lib.
 #ifndef DEFAULT_LIBPATH
 #define DEFAULT_LIBPATH "/lib:/usr/lib"
 #endif
 
+// Base path of extensions installed on the system.
+#define SYS_EXT_DIR     "/usr/java/packages"
 #define EXTENSIONS_DIR  "/lib/ext"
 #define ENDORSED_DIR    "/lib/endorsed"
-#define REG_DIR         "/usr/java/packages"
 
-#ifdef __APPLE__
+#ifndef __APPLE__
+
+  // Buffer that fits several sprintfs.
+  // Note that the space for the colon and the trailing null are provided
+  // by the nulls included by the sizeof operator.
+  const size_t bufsize =
+    MAX4((size_t)MAXPATHLEN,  // For dll_dir & friends.
+         sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH), // invariant ld_library_path
+         (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir
+         (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir
+  char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
+
+  // sysclasspath, java_home, dll_dir
+  {
+    char *pslash;
+    os::jvm_path(buf, bufsize);
+
+    // 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.
+  //
+  // Note: Due to a legacy implementation, most of the library path
+  // is set in the launcher. This was to accomodate linking restrictions
+  // on legacy Bsd implementations (which are no longer supported).
+  // Eventually, all the library path setting will be done here.
+  //
+  // However, to prevent the proliferation of improperly built native
+  // libraries, the new path component /usr/java/packages is added here.
+  // Eventually, all the library path setting will be done here.
+  {
+    char *ld_library_path_inv = buf;
+
+    // Construct the invariant part of ld_library_path.
+    sprintf(ld_library_path_inv, SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
+
+    // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
+    // should always exist (until the legacy problem cited above is
+    // addressed).
+    char *v = ::getenv("LD_LIBRARY_PATH");
+    if (v != NULL) {
+      // That's +1 for the colon and +1 for the trailing '\0'.
+      char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + strlen(ld_library_path_inv) + 1, mtInternal);
+      sprintf(ld_library_path, "%s:%s", v, ld_library_path_inv);
+      Arguments::set_library_path(ld_library_path);
+      FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
+    } else {
+      Arguments::set_library_path(ld_library_path_inv);
+    }
+  }
+
+  // Extensions directories.
+  sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_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);
+
+  FREE_C_HEAP_ARRAY(char, buf, mtInternal);
+
+#else // __APPLE__
+
 #define SYS_EXTENSIONS_DIR   "/Library/Java/Extensions"
 #define SYS_EXTENSIONS_DIRS  SYS_EXTENSIONS_DIR ":/Network" SYS_EXTENSIONS_DIR ":/System" SYS_EXTENSIONS_DIR ":/usr/lib/java"
+
         const char *user_home_dir = get_home();
-        // the null in SYS_EXTENSIONS_DIRS counts for the size of the colon after user_home_dir
-        int system_ext_size = strlen(user_home_dir) + sizeof(SYS_EXTENSIONS_DIR) +
+  // The null in SYS_EXTENSIONS_DIRS counts for the size of the colon after user_home_dir.
+  size_t system_ext_size = strlen(user_home_dir) + sizeof(SYS_EXTENSIONS_DIR) +
             sizeof(SYS_EXTENSIONS_DIRS);
-#endif
 
+  // Buffer that fits several sprintfs.
+  // Note that the space for the colon and the trailing null are provided
+  // by the nulls included by the sizeof operator.
+  const size_t bufsize =
+    MAX4((size_t)MAXPATHLEN,  // for dll_dir & friends.
+         system_ext_size, // reg dir
+         (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + system_ext_size, // extensions dir
+         (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir
+  char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
+
+  // sysclasspath, java_home, dll_dir
   {
-    /* sysclasspath, java_home, dll_dir */
-    {
-        char *home_path;
-        char *dll_path;
         char *pslash;
-        char buf[MAXPATHLEN];
-        os::jvm_path(buf, sizeof(buf));
+    os::jvm_path(buf, bufsize);
 
         // 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 */
+    *(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 = '\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> (/lib on macosx) */
-#ifndef __APPLE__
-                pslash = strrchr(buf, '/');
-                if (pslash != NULL)
-                    *pslash = '\0';   /* get rid of /lib */
-#endif
+        *pslash = '\0';          // Get rid of /lib.
             }
         }
+    Arguments::set_java_home(buf);
 
-        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('/', ':'))
+    if (!set_boot_path('/', ':')) {
             return;
     }
+  }
 
-    /*
-     * Where to look for native libraries
-     *
-     * Note: Due to a legacy implementation, most of the library path
-     * is set in the launcher.  This was to accomodate linking restrictions
-     * on legacy Bsd implementations (which are no longer supported).
-     * Eventually, all the library path setting will be done here.
-     *
-     * However, to prevent the proliferation of improperly built native
-     * libraries, the new path component /usr/java/packages is added here.
-     * Eventually, all the library path setting will be done here.
-     */
+  // Where to look for native libraries.
+  //
+  // Note: Due to a legacy implementation, most of the library path
+  // is set in the launcher. This was to accomodate linking restrictions
+  // on legacy Bsd implementations (which are no longer supported).
+  // Eventually, all the library path setting will be done here.
+  //
+  // However, to prevent the proliferation of improperly built native
+  // libraries, the new path component /usr/java/packages is added here.
+  // Eventually, all the library path setting will be done here.
     {
-        char *ld_library_path;
+    char *ld_library_path = buf;
 
-        /*
-         * 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 (so actually we allocate
-         * a byte more than necessary).
-         */
-#ifdef __APPLE__
-        ld_library_path = (char *) malloc(system_ext_size);
+    // Construct the invariant part of ld_library_path.
         sprintf(ld_library_path, "%s" SYS_EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS, user_home_dir);
-#else
-        ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") +
-            strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH));
-        sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
-#endif
 
-        /*
-         * Get the user setting of LD_LIBRARY_PATH, and prepended it.  It
-         * should always exist (until the legacy problem cited above is
-         * addressed).
-         */
-#ifdef __APPLE__
-        // Prepend the default path with the JAVA_LIBRARY_PATH so that the app launcher code can specify a directory inside an app wrapper
-        char *l = getenv("JAVA_LIBRARY_PATH");
+    // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
+    // should always exist (until the legacy problem cited above is
+    // addressed).
+    // Prepend the default path with the JAVA_LIBRARY_PATH so that the app launcher code
+    // can specify a directory inside an app wrapper
+    char *l = ::getenv("JAVA_LIBRARY_PATH");
         if (l != NULL) {
             char *t = ld_library_path;
-            /* That's +1 for the colon and +1 for the trailing '\0' */
-            ld_library_path = (char *) malloc(strlen(l) + 1 + strlen(t) + 1);
-            sprintf(ld_library_path, "%s:%s", l, t);
-            free(t);
+      // That's +1 for the colon and +1 for the trailing '\0'.
+      ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(l) + 1 + strlen(t) + 1, mtInternal);
+      sprintf(ld_library_path, "%s:%s", l, ld_library_path);
+      // Don't free t, it's buf which is used further down.
         }
 
-        char *v = getenv("DYLD_LIBRARY_PATH");
-#else
-        char *v = getenv("LD_LIBRARY_PATH");
-#endif
+    char *v = ::getenv("DYLD_LIBRARY_PATH");
         if (v != NULL) {
             char *t = ld_library_path;
-            /* That's +1 for the colon and +1 for the trailing '\0' */
-            ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1);
+      // That's +1 for the colon and +1 for the trailing '\0'.
+      ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + strlen(t) + 1, mtInternal);
             sprintf(ld_library_path, "%s:%s", v, t);
-            free(t);
+      if (t != buf) { FREE_C_HEAP_ARRAY(char, t, mtInternal); }
         }
 
-#ifdef __APPLE__
         // Apple's Java6 has "." at the beginning of java.library.path.
         // OpenJDK on Windows has "." at the end of java.library.path.
         // OpenJDK on Linux and Solaris don't have "." in java.library.path
         // at all. To ease the transition from Apple's Java6 to OpenJDK7,
         // "." is appended to the end of java.library.path. Yes, this

@@ -475,61 +526,42 @@
         // can be achieved by putting "." at the beginning of the
         // JAVA_LIBRARY_PATH environment variable.
         {
             char *t = ld_library_path;
             // that's +3 for appending ":." and the trailing '\0'
-            ld_library_path = (char *) malloc(strlen(t) + 3);
+      ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(t) + 3, mtInternal);
             sprintf(ld_library_path, "%s:%s", t, ".");
-            free(t);
+      if (t != buf) { FREE_C_HEAP_ARRAY(char, t, mtInternal); }
         }
-#endif
 
         Arguments::set_library_path(ld_library_path);
+    if (ld_library_path != buf) { FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal); }
     }
 
-    /*
-     * 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).
-     */
-    {
-#ifdef __APPLE__
-        char *buf = malloc(strlen(Arguments::get_java_home()) +
-            sizeof(EXTENSIONS_DIR) + system_ext_size);
-        sprintf(buf, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":"
-            SYS_EXTENSIONS_DIRS, user_home_dir, Arguments::get_java_home());
-#else
-        char *buf = malloc(strlen(Arguments::get_java_home()) +
-            sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR));
-        sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR,
-            Arguments::get_java_home());
-#endif
-
+  // 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).
+  sprintf(buf, "%s" SYS_EXTENSIONS_DIR ":%s" EXTENSIONS_DIR ":" SYS_EXTENSIONS_DIRS,
+          user_home_dir, Arguments::get_java_home());
         Arguments::set_ext_dirs(buf);
-    }
 
-    /* Endorsed standards default directory. */
-    {
-        char * buf;
-        buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
+  // Endorsed standards default directory.
         sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
         Arguments::set_endorsed_dirs(buf);
-    }
-  }
 
-#ifdef __APPLE__
+  FREE_C_HEAP_ARRAY(char, buf, mtInternal);
+
 #undef SYS_EXTENSIONS_DIR
-#endif
-#undef malloc
-#undef getenv
+#undef SYS_EXTENSIONS_DIRS
+
+#endif // __APPLE__
+
+#undef SYS_EXT_DIR
 #undef EXTENSIONS_DIR
 #undef ENDORSED_DIR
-
-  // Done
-  return;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // breakpoint support