src/solaris/back/linker_md.c

Print this page
rev 6659 : 8009558: linked_md.c::dll_build_name can get stuck in an infinite loop
Reviewed-by:


  38 
  39 #include <stdio.h>
  40 #include <dlfcn.h>
  41 #include <unistd.h>
  42 #include <stdlib.h>
  43 #include <string.h>
  44 
  45 #include "path_md.h"
  46 #ifndef NATIVE
  47 #include "iomgr.h"
  48 #include "threads_md.h"
  49 #endif
  50 
  51 #ifdef __APPLE__
  52 #define LIB_SUFFIX "dylib"
  53 #else
  54 #define LIB_SUFFIX "so"
  55 #endif
  56 
  57 static void dll_build_name(char* buffer, size_t buflen,
  58                            const char* pname, const char* fname) {
  59     // Based on os_solaris.cpp
  60 
  61     char *path_sep = PATH_SEPARATOR;
  62     char *pathname = (char *)pname;
  63     *buffer = '\0';
  64     while (strlen(pathname) > 0) {
  65         char *p = strchr(pathname, *path_sep);
  66         if (p == NULL) {
  67             p = pathname + strlen(pathname);
  68         }
  69         /* check for NULL path */
  70         if (p == pathname) {
  71             continue;
  72         }
  73         (void)snprintf(buffer, buflen, "%.*s/lib%s." LIB_SUFFIX, (int)(p - pathname),
  74                        pathname, fname);
  75 





  76         if (access(buffer, F_OK) == 0) {
  77             break;
  78         }
  79         if (*p == '\0') {
  80             pathname = p;
  81         } else {
  82             pathname = p + 1;
  83         }
  84         *buffer = '\0';

  85     }


  86 }
  87 
  88 /*
  89  * create a string for the JNI native function name by adding the
  90  * appropriate decorations.
  91  */
  92 int
  93 dbgsysBuildFunName(char *name, int nameLen, int args_size, int encodingIndex)
  94 {
  95   /* On Solaris, there is only one encoding method. */
  96     if (encodingIndex == 0)
  97         return 1;
  98     return 0;
  99 }
 100 
 101 /*
 102  * create a string for the dynamic lib open call by adding the
 103  * appropriate pre and extensions to a filename and the path
 104  */
 105 void
 106 dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname)
 107 {
 108     const int pnamelen = pname ? strlen(pname) : 0;
 109 
 110     *holder = '\0';
 111     /* Quietly truncate on buffer overflow.  Should be an error. */
 112     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
 113         return;
 114     }
 115 
 116     if (pnamelen == 0) {
 117         (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
 118     } else {
 119       dll_build_name(holder, holderlen, pname, fname);
 120     }
 121 }
 122 
 123 #ifndef NATIVE
 124 extern int thr_main(void);
 125 #endif
 126 




  38 
  39 #include <stdio.h>
  40 #include <dlfcn.h>
  41 #include <unistd.h>
  42 #include <stdlib.h>
  43 #include <string.h>
  44 
  45 #include "path_md.h"
  46 #ifndef NATIVE
  47 #include "iomgr.h"
  48 #include "threads_md.h"
  49 #endif
  50 
  51 #ifdef __APPLE__
  52 #define LIB_SUFFIX "dylib"
  53 #else
  54 #define LIB_SUFFIX "so"
  55 #endif
  56 
  57 static void dll_build_name(char* buffer, size_t buflen,
  58                            const char* paths, const char* fname) {
  59     char *path, *paths_copy, *next_token;
  60 
  61     paths_copy = strdup(paths);
  62     if (paths_copy == NULL) {
  63         return;








  64     }


  65 
  66     next_token = NULL;
  67     path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
  68 
  69     while(path != NULL) {
  70         snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
  71         if (access(buffer, F_OK) == 0) {
  72             break;
  73         }





  74         *buffer = '\0';
  75         path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
  76     }
  77 
  78     free(paths_copy);
  79 }
  80 
  81 /*
  82  * create a string for the JNI native function name by adding the
  83  * appropriate decorations.
  84  */
  85 int
  86 dbgsysBuildFunName(char *name, int nameLen, int args_size, int encodingIndex)
  87 {
  88   /* On Solaris, there is only one encoding method. */
  89     if (encodingIndex == 0)
  90         return 1;
  91     return 0;
  92 }
  93 
  94 /*
  95  * create a string for the dynamic lib open call by adding the
  96  * appropriate pre and extensions to a filename and the path
  97  */
  98 void
  99 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
 100 {
 101     const int pnamelen = pname ? strlen(pname) : 0;
 102 
 103     *holder = '\0';
 104     /* Quietly truncate on buffer overflow.  Should be an error. */
 105     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
 106         return;
 107     }
 108 
 109     if (pnamelen == 0) {
 110         (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
 111     } else {
 112       dll_build_name(holder, holderlen, pname, fname);
 113     }
 114 }
 115 
 116 #ifndef NATIVE
 117 extern int thr_main(void);
 118 #endif
 119