8009558: linked_md.c::dll_build_name can get stuck in an infinite loop

Reviewed-by: alanb, sspitsyn
This commit is contained in:
Staffan Larsen 2013-04-02 10:32:21 +02:00
parent 7d45058eaa
commit bce70c8f7b
7 changed files with 73 additions and 107 deletions

View File

@ -37,7 +37,7 @@
/* Implemented in linker_md.c */ /* 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 * dbgsysLoadLibrary(const char *, char *err_buf, int err_buflen);
void dbgsysUnloadLibrary(void *); void dbgsysUnloadLibrary(void *);
void * dbgsysFindLibraryEntry(void *, const char *); void * dbgsysFindLibraryEntry(void *, const char *);

View File

@ -97,12 +97,12 @@ findTransportOnLoad(void *handle)
/* Load transport library (directory=="" means do system search) */ /* Load transport library (directory=="" means do system search) */
static void * static void *
loadTransportLibrary(char *libdir, char *name) loadTransportLibrary(const char *libdir, const char *name)
{ {
void *handle; void *handle;
char libname[MAXPATHLEN+2]; char libname[MAXPATHLEN+2];
char buf[MAXPATHLEN*2+100]; char buf[MAXPATHLEN*2+100];
char *plibdir; const char *plibdir;
/* Convert libdir from UTF-8 to platform encoding */ /* Convert libdir from UTF-8 to platform encoding */
plibdir = NULL; plibdir = NULL;
@ -131,12 +131,12 @@ loadTransportLibrary(char *libdir, char *name)
* JDK 1.2 javai.c v1.61 * JDK 1.2 javai.c v1.61
*/ */
static jdwpError static jdwpError
loadTransport(char *name, jdwpTransportEnv **transportPtr) loadTransport(const char *name, jdwpTransportEnv **transportPtr)
{ {
JNIEnv *env; JNIEnv *env;
jdwpTransport_OnLoad_t onLoad; jdwpTransport_OnLoad_t onLoad;
void *handle; void *handle;
char *libdir; const char *libdir;
/* Make sure library name is not empty */ /* Make sure library name is not empty */
if (name == NULL) { if (name == NULL) {

View File

@ -69,7 +69,7 @@ unsigned md_htonl(unsigned l);
unsigned md_ntohs(unsigned short s); unsigned md_ntohs(unsigned short s);
unsigned md_ntohl(unsigned l); 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_load_library(const char *name, char *err_buf, int err_buflen);
void md_unload_library(void *handle); void md_unload_library(void *handle);
void * md_find_library_entry(void *handle, const char *name); void * md_find_library_entry(void *handle, const char *name);

View File

@ -55,34 +55,27 @@
#endif #endif
static void dll_build_name(char* buffer, size_t buflen, static void dll_build_name(char* buffer, size_t buflen,
const char* pname, const char* fname) { const char* paths, const char* fname) {
// Based on os_solaris.cpp char *path, *paths_copy, *next_token;
char *path_sep = PATH_SEPARATOR; paths_copy = strdup(paths);
char *pathname = (char *)pname; if (paths_copy == NULL) {
*buffer = '\0'; return;
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);
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) { if (access(buffer, F_OK) == 0) {
break; break;
} }
if (*p == '\0') {
pathname = p;
} else {
pathname = p + 1;
}
*buffer = '\0'; *buffer = '\0';
path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
} }
free(paths_copy);
} }
/* /*
@ -103,7 +96,7 @@ dbgsysBuildFunName(char *name, int nameLen, int args_size, int encodingIndex)
* appropriate pre and extensions to a filename and the path * appropriate pre and extensions to a filename and the path
*/ */
void 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; const int pnamelen = pname ? strlen(pname) : 0;

View File

@ -381,38 +381,32 @@ md_ntohl(unsigned l)
} }
static void dll_build_name(char* buffer, size_t buflen, static void dll_build_name(char* buffer, size_t buflen,
const char* pname, const char* fname) { const char* paths, const char* fname) {
// Loosely based on os_solaris.cpp char *path, *paths_copy, *next_token;
char *pathname = (char *)pname; paths_copy = strdup(paths);
*buffer = '\0'; if (paths_copy == NULL) {
while (strlen(pathname) > 0) { return;
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);
if (access(buffer, F_OK) == 0) { next_token = NULL;
break; path = strtok_r(paths_copy, ":", &next_token);
}
if (*p == '\0') { while (path != NULL) {
pathname = p; snprintf(buffer, buflen, "%s/lib%s" JNI_LIB_SUFFIX, path, fname);
} else { if (access(buffer, F_OK) == 0) {
pathname = p + 1; break;
} }
*buffer = '\0'; *buffer = '\0';
} path = strtok_r(NULL, ":", &next_token);
}
free(paths_copy);
} }
/* Create the actual fill filename for a dynamic library. */ /* Create the actual fill filename for a dynamic library. */
void 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; int pnamelen;

View File

@ -39,38 +39,27 @@
#include "path_md.h" #include "path_md.h"
static void dll_build_name(char* buffer, size_t buflen, static void dll_build_name(char* buffer, size_t buflen,
const char* pname, const char* fname) { const char* paths, const char* fname) {
// Based on os_windows.cpp char *path, *paths_copy, *next_token;
char *path_sep = PATH_SEPARATOR; paths_copy = strdup(paths);
char *pathname = (char *)pname; if (paths_copy == NULL) {
*buffer = '\0'; return;
while (strlen(pathname) > 0) { }
char *p = strchr(pathname, *path_sep);
if (p == NULL) { next_token = NULL;
p = pathname + strlen(pathname); path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token);
}
/* check for NULL path */ while (path != NULL) {
if (p == pathname) { _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
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);
}
if (_access(buffer, 0) == 0) { if (_access(buffer, 0) == 0) {
break; break;
} }
if (*p == '\0') {
pathname = p;
} else {
pathname = p + 1;
}
*buffer = '\0'; *buffer = '\0';
path = strtok_s(NULL, PATH_SEPARATOR, &next_token);
} }
free(paths_copy);
} }
/* /*
@ -113,7 +102,7 @@ dbgsysGetLastErrorString(char *buf, int len)
* Build a machine dependent library name out of a path and file name. * Build a machine dependent library name out of a path and file name.
*/ */
void 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; const int pnamelen = pname ? (int)strlen(pname) : 0;

View File

@ -368,42 +368,32 @@ get_last_error_string(char *buf, int len)
} }
static void dll_build_name(char* buffer, size_t buflen, static void dll_build_name(char* buffer, size_t buflen,
const char* pname, const char* fname) { const char* paths, const char* fname) {
// Loosley based on os_windows.cpp char *path, *paths_copy, *next_token;
char *pathname = (char *)pname; paths_copy = strdup(paths);
*buffer = '\0'; if (paths_copy == NULL) {
while (strlen(pathname) > 0) { return;
char *p = strchr(pathname, ';'); }
if (p == NULL) {
p = pathname + strlen(pathname); next_token = NULL;
} path = strtok_s(paths_copy, ";", &next_token);
/* check for NULL path */
if (p == pathname) { while (path != NULL) {
continue; _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
}
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);
}
if (_access(buffer, 0) == 0) { if (_access(buffer, 0) == 0) {
break; break;
} }
if (*p == '\0') {
pathname = p;
} else {
pathname = p + 1;
}
*buffer = '\0'; *buffer = '\0';
path = strtok_s(NULL, ";", &next_token);
} }
free(paths_copy);
} }
/* Build a machine dependent library name out of a path and file name. */ /* Build a machine dependent library name out of a path and file name. */
void 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; int pnamelen;