src/os/aix/vm/os_aix.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.

@@ -58,30 +58,20 @@
 #include "runtime/osThread.hpp"
 #include "runtime/perfMemory.hpp"
 #include "runtime/sharedRuntime.hpp"
 #include "runtime/statSampler.hpp"
 #include "runtime/stubRoutines.hpp"
-#include "runtime/threadCritical.hpp"
 #include "runtime/thread.inline.hpp"
+#include "runtime/threadCritical.hpp"
 #include "runtime/timer.hpp"
 #include "services/attachListener.hpp"
 #include "services/runtimeService.hpp"
 #include "utilities/decoder.hpp"
 #include "utilities/defaultStream.hpp"
 #include "utilities/events.hpp"
 #include "utilities/growableArray.hpp"
 #include "utilities/vmError.hpp"
-#ifdef TARGET_ARCH_ppc
-# include "assembler_ppc.inline.hpp"
-# include "nativeInst_ppc.hpp"
-#endif
-#ifdef COMPILER1
-#include "c1/c1_Runtime1.hpp"
-#endif
-#ifdef COMPILER2
-#include "opto/runtime.hpp"
-#endif
 
 // put OS-includes here (sorted alphabetically)
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>

@@ -376,17 +366,18 @@
 
   // This really would surprise me.
   assert(_page_size == SIZE_4K, "surprise!");
 
 
-  // query default data page size (default page size for C-Heap, pthread stacks and .bss).
+  // Query default data page size (default page size for C-Heap, pthread stacks and .bss).
   // Default data page size is influenced either by linker options (-bdatapsize)
   // or by environment variable LDR_CNTRL (suboption DATAPSIZE). If none is given,
   // default should be 4K.
   size_t data_page_size = SIZE_4K;
   {
     void* p = ::malloc(SIZE_16M);
+    guarantee(p != NULL, "malloc failed");
     data_page_size = os::Aix::query_pagesize(p);
     ::free(p);
   }
 
   // query default shm page size (LDR_CNTRL SHMPSIZE)

@@ -509,89 +500,80 @@
     fprintf(stderr, "Multipage error details: %d\n", g_multipage_error);
   }
 
 } // end os::Aix::query_multipage_support()
 
-
-// The code for this method was initially derived from the version in os_linux.cpp
+// The code for this method was initially derived from the version in os_linux.cpp.
 void os::init_system_properties_values() {
-  // The next few definitions allow the code to be verbatim:
-#define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n), mtInternal)
+
 #define DEFAULT_LIBPATH "/usr/lib:/lib"
 #define EXTENSIONS_DIR  "/lib/ext"
 #define ENDORSED_DIR    "/lib/endorsed"
 
   // 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
+    *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.
   pslash = strrchr(buf, '/');
   if (pslash != NULL) {
-    *pslash = '\0';            // get rid of /{client|server|hotspot}
+      *pslash = '\0';            // Get rid of /{client|server|hotspot}.
   }
-
-  dll_path = malloc(strlen(buf) + 1);
-  strcpy(dll_path, buf);
-  Arguments::set_dll_dir(dll_path);
+    Arguments::set_dll_dir(buf);
 
   if (pslash != NULL) {
     pslash = strrchr(buf, '/');
     if (pslash != NULL) {
-      *pslash = '\0';          // get rid of /<arch>
+        *pslash = '\0';          // Get rid of /<arch>.
       pslash = strrchr(buf, '/');
       if (pslash != NULL) {
-        *pslash = '\0';        // get rid of /lib
+          *pslash = '\0';        // Get rid of /lib.
       }
     }
   }
+    Arguments::set_java_home(buf);
 
-  home_path = malloc(strlen(buf) + 1);
-  strcpy(home_path, buf);
-  Arguments::set_java_home(home_path);
-
-  if (!set_boot_path('/', ':')) return;
+    if (!set_boot_path('/', ':')) {
+      return;
+    }
+  }
 
-  // Where to look for native libraries
+  // Where to look for native libraries.
 
-  // On Aix we get the user setting of LIBPATH
+  // On Aix we get the user setting of LIBPATH.
   // Eventually, all the library path setting will be done here.
-  char *ld_library_path;
-
-  // Construct the invariant part of ld_library_path.
-  ld_library_path = (char *) malloc(sizeof(DEFAULT_LIBPATH));
-  sprintf(ld_library_path, DEFAULT_LIBPATH);
-
-  // Get the user setting of LIBPATH, and prepended it.
+  // Get the user setting of LIBPATH.
   char *v = ::getenv("LIBPATH");
   if (v == NULL) {
     v = "";
   }
 
-  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);
-  sprintf(ld_library_path, "%s:%s", v, t);
-
+  // Concatenate user and invariant part of ld_library_path.
+  // 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 + sizeof(DEFAULT_LIBPATH) + 1, mtInternal);
+  sprintf(ld_library_path, "%s:%s", v, DEFAULT_LIBPATH);
   Arguments::set_library_path(ld_library_path);
+  FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
 
-  // Extensions directories
-  char* cbuf = malloc(strlen(Arguments::get_java_home()) + sizeof(EXTENSIONS_DIR));
-  sprintf(cbuf, "%s" EXTENSIONS_DIR, Arguments::get_java_home());
-  Arguments::set_ext_dirs(cbuf);
+  // 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(ENDORSED_DIR)];
+
+  // Extensions directories.
+  sprintf(buf, "%s" EXTENSIONS_DIR, Arguments::get_java_home());
+  Arguments::set_ext_dirs(buf);
 
   // Endorsed standards default directory.
-  cbuf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
-  sprintf(cbuf, "%s" ENDORSED_DIR, Arguments::get_java_home());
-  Arguments::set_endorsed_dirs(cbuf);
+  sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
+  Arguments::set_endorsed_dirs(buf);
 
-#undef malloc
 #undef DEFAULT_LIBPATH
 #undef EXTENSIONS_DIR
 #undef ENDORSED_DIR
 }