8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
authorsla
Tue, 02 Apr 2013 10:32:21 +0200
changeset 16726 f76b2e6bd199
parent 16725 9e7cfcc4ff6a
child 16727 1d83fd280379
8009558: linked_md.c::dll_build_name can get stuck in an infinite loop Reviewed-by: alanb, sspitsyn
jdk/src/share/back/export/sys.h
jdk/src/share/back/transport.c
jdk/src/share/demo/jvmti/hprof/hprof_md.h
jdk/src/solaris/back/linker_md.c
jdk/src/solaris/demo/jvmti/hprof/hprof_md.c
jdk/src/windows/back/linker_md.c
jdk/src/windows/demo/jvmti/hprof/hprof_md.c
--- a/jdk/src/share/back/export/sys.h	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/share/back/export/sys.h	Tue Apr 02 10:32:21 2013 +0200
@@ -37,7 +37,7 @@
 
 /* Implemented in linker_md.c */
 
-void    dbgsysBuildLibName(char *, int, char *, char *);
+void    dbgsysBuildLibName(char *, int, const char *, const char *);
 void *  dbgsysLoadLibrary(const char *, char *err_buf, int err_buflen);
 void    dbgsysUnloadLibrary(void *);
 void *  dbgsysFindLibraryEntry(void *, const char *);
--- a/jdk/src/share/back/transport.c	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/share/back/transport.c	Tue Apr 02 10:32:21 2013 +0200
@@ -97,12 +97,12 @@
 
 /* Load transport library (directory=="" means do system search) */
 static void *
-loadTransportLibrary(char *libdir, char *name)
+loadTransportLibrary(const char *libdir, const char *name)
 {
     void *handle;
     char libname[MAXPATHLEN+2];
     char buf[MAXPATHLEN*2+100];
-    char *plibdir;
+    const char *plibdir;
 
     /* Convert libdir from UTF-8 to platform encoding */
     plibdir = NULL;
@@ -131,12 +131,12 @@
  * JDK 1.2 javai.c v1.61
  */
 static jdwpError
-loadTransport(char *name, jdwpTransportEnv **transportPtr)
+loadTransport(const char *name, jdwpTransportEnv **transportPtr)
 {
     JNIEnv                 *env;
     jdwpTransport_OnLoad_t  onLoad;
     void                   *handle;
-    char                   *libdir;
+    const char             *libdir;
 
     /* Make sure library name is not empty */
     if (name == NULL) {
--- a/jdk/src/share/demo/jvmti/hprof/hprof_md.h	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/share/demo/jvmti/hprof/hprof_md.h	Tue Apr 02 10:32:21 2013 +0200
@@ -69,7 +69,7 @@
 unsigned md_ntohs(unsigned short s);
 unsigned md_ntohl(unsigned l);
 
-void   md_build_library_name(char *holder, int holderlen, char *pname, char *fname);
+void   md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname);
 void * md_load_library(const char *name, char *err_buf, int err_buflen);
 void   md_unload_library(void *handle);
 void * md_find_library_entry(void *handle, const char *name);
--- a/jdk/src/solaris/back/linker_md.c	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/solaris/back/linker_md.c	Tue Apr 02 10:32:21 2013 +0200
@@ -55,34 +55,27 @@
 #endif
 
 static void dll_build_name(char* buffer, size_t buflen,
-                           const char* pname, const char* fname) {
-    // Based on os_solaris.cpp
+                           const char* paths, const char* fname) {
+    char *path, *paths_copy, *next_token;
 
-    char *path_sep = PATH_SEPARATOR;
-    char *pathname = (char *)pname;
-    *buffer = '\0';
-    while (strlen(pathname) > 0) {
-        char *p = strchr(pathname, *path_sep);
-        if (p == NULL) {
-            p = pathname + strlen(pathname);
-        }
-        /* check for NULL path */
-        if (p == pathname) {
-            continue;
-        }
-        (void)snprintf(buffer, buflen, "%.*s/lib%s." LIB_SUFFIX, (int)(p - pathname),
-                       pathname, fname);
+    paths_copy = strdup(paths);
+    if (paths_copy == NULL) {
+        return;
+    }
 
+    next_token = NULL;
+    path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
+
+    while (path != NULL) {
+        snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
         if (access(buffer, F_OK) == 0) {
             break;
         }
-        if (*p == '\0') {
-            pathname = p;
-        } else {
-            pathname = p + 1;
-        }
         *buffer = '\0';
+        path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
     }
+
+    free(paths_copy);
 }
 
 /*
@@ -103,7 +96,7 @@
  * appropriate pre and extensions to a filename and the path
  */
 void
-dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname)
+dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
 {
     const int pnamelen = pname ? strlen(pname) : 0;
 
--- a/jdk/src/solaris/demo/jvmti/hprof/hprof_md.c	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/solaris/demo/jvmti/hprof/hprof_md.c	Tue Apr 02 10:32:21 2013 +0200
@@ -381,38 +381,32 @@
 }
 
 static void dll_build_name(char* buffer, size_t buflen,
-                           const char* pname, const char* fname) {
-    // Loosely based on os_solaris.cpp
+                           const char* paths, const char* fname) {
+    char *path, *paths_copy, *next_token;
+
+    paths_copy = strdup(paths);
+    if (paths_copy == NULL) {
+        return;
+    }
+
+    next_token = NULL;
+    path = strtok_r(paths_copy, ":", &next_token);
 
-      char *pathname = (char *)pname;
-      *buffer = '\0';
-      while (strlen(pathname) > 0) {
-          char *p = strchr(pathname, ':');
-          if (p == NULL) {
-              p = pathname + strlen(pathname);
-          }
-          /* check for NULL path */
-          if (p == pathname) {
-              continue;
-          }
-          (void)snprintf(buffer, buflen, "%.*s/lib%s" JNI_LIB_SUFFIX,
-                         (int)(p - pathname), pathname, fname);
+    while (path != NULL) {
+        snprintf(buffer, buflen, "%s/lib%s" JNI_LIB_SUFFIX, path, fname);
+        if (access(buffer, F_OK) == 0) {
+            break;
+        }
+        *buffer = '\0';
+        path = strtok_r(NULL, ":", &next_token);
+    }
 
-          if (access(buffer, F_OK) == 0) {
-              break;
-          }
-          if (*p == '\0') {
-              pathname = p;
-          } else {
-              pathname = p + 1;
-          }
-          *buffer = '\0';
-      }
+    free(paths_copy);
 }
 
 /* Create the actual fill filename for a dynamic library.  */
 void
-md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
+md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)
 {
     int   pnamelen;
 
--- a/jdk/src/windows/back/linker_md.c	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/windows/back/linker_md.c	Tue Apr 02 10:32:21 2013 +0200
@@ -39,38 +39,27 @@
 #include "path_md.h"
 
 static void dll_build_name(char* buffer, size_t buflen,
-                           const char* pname, const char* fname) {
-    // Based on os_windows.cpp
+                           const char* paths, const char* fname) {
+    char *path, *paths_copy, *next_token;
 
-    char *path_sep = PATH_SEPARATOR;
-    char *pathname = (char *)pname;
-    *buffer = '\0';
-    while (strlen(pathname) > 0) {
-        char *p = strchr(pathname, *path_sep);
-        if (p == NULL) {
-            p = pathname + strlen(pathname);
-        }
-        /* check for NULL path */
-        if (p == pathname) {
-            continue;
-        }
-        if (*(p-1) == ':' || *(p-1) == '\\') {
-            (void)_snprintf(buffer, buflen, "%.*s%s.dll", (int)(p - pathname),
-                            pathname, fname);
-        } else {
-            (void)_snprintf(buffer, buflen, "%.*s\\%s.dll", (int)(p - pathname),
-                            pathname, fname);
-        }
+    paths_copy = strdup(paths);
+    if (paths_copy == NULL) {
+        return;
+    }
+
+    next_token = NULL;
+    path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token);
+
+    while (path != NULL) {
+        _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
         if (_access(buffer, 0) == 0) {
             break;
         }
-        if (*p == '\0') {
-            pathname = p;
-        } else {
-            pathname = p + 1;
-        }
         *buffer = '\0';
+        path = strtok_s(NULL, PATH_SEPARATOR, &next_token);
     }
+
+    free(paths_copy);
 }
 
 /*
@@ -113,7 +102,7 @@
  * Build a machine dependent library name out of a path and file name.
  */
 void
-dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname)
+dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
 {
     const int pnamelen = pname ? (int)strlen(pname) : 0;
 
--- a/jdk/src/windows/demo/jvmti/hprof/hprof_md.c	Mon Apr 01 20:51:40 2013 -0700
+++ b/jdk/src/windows/demo/jvmti/hprof/hprof_md.c	Tue Apr 02 10:32:21 2013 +0200
@@ -368,42 +368,32 @@
 }
 
 static void dll_build_name(char* buffer, size_t buflen,
-                           const char* pname, const char* fname) {
-    // Loosley based on os_windows.cpp
+                           const char* paths, const char* fname) {
+    char *path, *paths_copy, *next_token;
 
-    char *pathname = (char *)pname;
-    *buffer = '\0';
-    while (strlen(pathname) > 0) {
-        char *p = strchr(pathname, ';');
-        if (p == NULL) {
-            p = pathname + strlen(pathname);
-        }
-        /* check for NULL path */
-        if (p == pathname) {
-            continue;
-        }
-        if (*(p-1) == ':' || *(p-1) == '\\') {
-          (void)_snprintf(buffer, buflen, "%.*s%s.dll", (int)(p - pathname),
-                            pathname, fname);
-        } else {
-          (void)_snprintf(buffer, buflen, "%.*s\\%s.dll", (int)(p - pathname),
-                            pathname, fname);
-        }
+    paths_copy = strdup(paths);
+    if (paths_copy == NULL) {
+        return;
+    }
+
+    next_token = NULL;
+    path = strtok_s(paths_copy, ";", &next_token);
+
+    while (path != NULL) {
+        _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
         if (_access(buffer, 0) == 0) {
             break;
         }
-        if (*p == '\0') {
-            pathname = p;
-        } else {
-            pathname = p + 1;
-        }
         *buffer = '\0';
+        path = strtok_s(NULL, ";", &next_token);
     }
+
+    free(paths_copy);
 }
 
 /* Build a machine dependent library name out of a path and file name.  */
 void
-md_build_library_name(char *holder, int holderlen, char *pname, char *fname)
+md_build_library_name(char *holder, int holderlen, const char *pname, const char *fname)
 {
     int   pnamelen;