src/jdk.jdwp.agent/unix/native/libjdwp/linker_md.c
changeset 58092 b00b4706ec0f
parent 54100 1a6a5a0260a7
equal deleted inserted replaced
58091:c0cc906cb29c 58092:b00b4706ec0f
    32 #include <dlfcn.h>
    32 #include <dlfcn.h>
    33 #include <unistd.h>
    33 #include <unistd.h>
    34 #include <stdlib.h>
    34 #include <stdlib.h>
    35 #include <string.h>
    35 #include <string.h>
    36 
    36 
       
    37 #include "util.h"
    37 #include "path_md.h"
    38 #include "path_md.h"
    38 
    39 
    39 #ifdef __APPLE__
    40 #ifdef __APPLE__
    40 #define LIB_SUFFIX "dylib"
    41 #define LIB_SUFFIX "dylib"
    41 #else
    42 #else
    43 #endif
    44 #endif
    44 
    45 
    45 static void dll_build_name(char* buffer, size_t buflen,
    46 static void dll_build_name(char* buffer, size_t buflen,
    46                            const char* paths, const char* fname) {
    47                            const char* paths, const char* fname) {
    47     char *path, *paths_copy, *next_token;
    48     char *path, *paths_copy, *next_token;
       
    49     *buffer = '\0';
    48 
    50 
    49     paths_copy = strdup(paths);
    51     paths_copy = jvmtiAllocate((int)strlen(paths) + 1);
       
    52     strcpy(paths_copy, paths);
    50     if (paths_copy == NULL) {
    53     if (paths_copy == NULL) {
    51         return;
    54         return;
    52     }
    55     }
    53 
    56 
    54     next_token = NULL;
    57     next_token = NULL;
    55     path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
    58     path = strtok_r(paths_copy, PATH_SEPARATOR, &next_token);
    56 
    59 
    57     while (path != NULL) {
    60     while (path != NULL) {
    58         snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
    61         size_t result_len = (size_t)snprintf(buffer, buflen, "%s/lib%s." LIB_SUFFIX, path, fname);
    59         if (access(buffer, F_OK) == 0) {
    62         if (result_len >= buflen) {
       
    63             EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
       
    64                                                      "likely by sun.boot.library.path, is too long.");
       
    65         } else if (access(buffer, F_OK) == 0) {
    60             break;
    66             break;
    61         }
    67         }
    62         *buffer = '\0';
    68         *buffer = '\0';
    63         path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
    69         path = strtok_r(NULL, PATH_SEPARATOR, &next_token);
    64     }
    70     }
    65 
    71 
    66     free(paths_copy);
    72     jvmtiDeallocate(paths_copy);
    67 }
    73 }
    68 
    74 
    69 /*
    75 /*
    70  * create a string for the JNI native function name by adding the
    76  * create a string for the JNI native function name by adding the
    71  * appropriate decorations.
    77  * appropriate decorations.
    87 dbgsysBuildLibName(char *holder, int holderlen, const char *pname,
    93 dbgsysBuildLibName(char *holder, int holderlen, const char *pname,
    88                    const char *fname)
    94                    const char *fname)
    89 {
    95 {
    90     const int pnamelen = pname ? strlen(pname) : 0;
    96     const int pnamelen = pname ? strlen(pname) : 0;
    91 
    97 
    92     *holder = '\0';
       
    93     // Quietly truncate on buffer overflow.  Should be an error.
       
    94     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
       
    95         return;
       
    96     }
       
    97 
       
    98     if (pnamelen == 0) {
    98     if (pnamelen == 0) {
       
    99         if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
       
   100             EXIT_ERROR(JVMTI_ERROR_INVALID_LOCATION, "One or more of the library paths supplied to jdwp, "
       
   101                                                      "likely by sun.boot.library.path, is too long.");
       
   102         }
    99         (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
   103         (void)snprintf(holder, holderlen, "lib%s." LIB_SUFFIX, fname);
   100     } else {
   104     } else {
   101       dll_build_name(holder, holderlen, pname, fname);
   105       dll_build_name(holder, holderlen, pname, fname);
   102     }
   106     }
   103 }
   107 }