src/windows/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:


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Maintains a list of currently loaded DLLs (Dynamic Link Libraries)
  28  * and their associated handles. Library names are case-insensitive.
  29  */
  30 
  31 #include <windows.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <errno.h>
  35 #include <io.h>
  36 
  37 #include "sys.h"
  38 
  39 #include "path_md.h"
  40 
  41 static void dll_build_name(char* buffer, size_t buflen,
  42                            const char* pname, const char* fname) {
  43     // Based on os_windows.cpp
  44 
  45     char *path_sep = PATH_SEPARATOR;
  46     char *pathname = (char *)pname;
  47     *buffer = '\0';
  48     while (strlen(pathname) > 0) {
  49         char *p = strchr(pathname, *path_sep);
  50         if (p == NULL) {
  51             p = pathname + strlen(pathname);
  52         }
  53         /* check for NULL path */
  54         if (p == pathname) {
  55             continue;
  56         }
  57         if (*(p-1) == ':' || *(p-1) == '\\') {
  58             (void)_snprintf(buffer, buflen, "%.*s%s.dll", (int)(p - pathname),
  59                             pathname, fname);
  60         } else {
  61             (void)_snprintf(buffer, buflen, "%.*s\\%s.dll", (int)(p - pathname),
  62                             pathname, fname);
  63         }






  64         if (_access(buffer, 0) == 0) {
  65             break;
  66         }
  67         if (*p == '\0') {
  68             pathname = p;
  69         } else {
  70             pathname = p + 1;
  71         }
  72         *buffer = '\0';

  73     }


  74 }
  75 
  76 /*
  77  * From system_md.c v1.54
  78  */
  79 int
  80 dbgsysGetLastErrorString(char *buf, int len)
  81 {
  82     long errval;
  83 
  84     if ((errval = GetLastError()) != 0) {
  85         /* DOS error */
  86         int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  87                               NULL, errval,
  88                               0, buf, len, NULL);
  89         if (n > 3) {
  90             /* Drop final '.', CR, LF */
  91             if (buf[n - 1] == '\n') n--;
  92             if (buf[n - 1] == '\r') n--;
  93             if (buf[n - 1] == '.') n--;


  96         return n;
  97     }
  98 
  99     if (errno != 0) {
 100         /* C runtime error that has no corresponding DOS error code */
 101         const char *s = strerror(errno);
 102         int n = (int)strlen(s);
 103         if (n >= len) n = len - 1;
 104         strncpy(buf, s, n);
 105         buf[n] = '\0';
 106         return n;
 107     }
 108 
 109     return 0;
 110 }
 111 
 112 /*
 113  * Build a machine dependent library name out of a path and file name.
 114  */
 115 void
 116 dbgsysBuildLibName(char *holder, int holderlen, char *pname, char *fname)
 117 {
 118     const int pnamelen = pname ? (int)strlen(pname) : 0;
 119 
 120     *holder = '\0';
 121     /* Quietly truncates on buffer overflow. Should be an error. */
 122     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
 123         return;
 124     }
 125 
 126     if (pnamelen == 0) {
 127         sprintf(holder, "%s.dll", fname);
 128     } else {
 129       dll_build_name(holder, holderlen, pname, fname);
 130     }
 131 }
 132 
 133 void *
 134 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen)
 135 {
 136     void *result = LoadLibrary(name);




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Maintains a list of currently loaded DLLs (Dynamic Link Libraries)
  28  * and their associated handles. Library names are case-insensitive.
  29  */
  30 
  31 #include <windows.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <errno.h>
  35 #include <io.h>
  36 
  37 #include "sys.h"
  38 
  39 #include "path_md.h"
  40 
  41 static void dll_build_name(char* buffer, size_t buflen,
  42                            const char* paths, const char* fname) {
  43     char *path, *paths_copy, *next_token;
  44 
  45     paths_copy = strdup(paths);
  46     if (paths_copy == NULL) {
  47         return;















  48     }
  49 
  50     next_token = NULL;
  51     path = strtok_s(paths_copy, PATH_SEPARATOR, &next_token);
  52 
  53     while(path != NULL) {
  54         _snprintf(buffer, buflen, "%s\\%s.dll", path, fname);
  55         if (_access(buffer, 0) == 0) {
  56             break;
  57         }





  58         *buffer = '\0';
  59         path = strtok_s(NULL, PATH_SEPARATOR, &next_token);
  60     }
  61 
  62     free(paths_copy);
  63 }
  64 
  65 /*
  66  * From system_md.c v1.54
  67  */
  68 int
  69 dbgsysGetLastErrorString(char *buf, int len)
  70 {
  71     long errval;
  72 
  73     if ((errval = GetLastError()) != 0) {
  74         /* DOS error */
  75         int n = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  76                               NULL, errval,
  77                               0, buf, len, NULL);
  78         if (n > 3) {
  79             /* Drop final '.', CR, LF */
  80             if (buf[n - 1] == '\n') n--;
  81             if (buf[n - 1] == '\r') n--;
  82             if (buf[n - 1] == '.') n--;


  85         return n;
  86     }
  87 
  88     if (errno != 0) {
  89         /* C runtime error that has no corresponding DOS error code */
  90         const char *s = strerror(errno);
  91         int n = (int)strlen(s);
  92         if (n >= len) n = len - 1;
  93         strncpy(buf, s, n);
  94         buf[n] = '\0';
  95         return n;
  96     }
  97 
  98     return 0;
  99 }
 100 
 101 /*
 102  * Build a machine dependent library name out of a path and file name.
 103  */
 104 void
 105 dbgsysBuildLibName(char *holder, int holderlen, const char *pname, const char *fname)
 106 {
 107     const int pnamelen = pname ? (int)strlen(pname) : 0;
 108 
 109     *holder = '\0';
 110     /* Quietly truncates on buffer overflow. Should be an error. */
 111     if (pnamelen + (int)strlen(fname) + 10 > holderlen) {
 112         return;
 113     }
 114 
 115     if (pnamelen == 0) {
 116         sprintf(holder, "%s.dll", fname);
 117     } else {
 118       dll_build_name(holder, holderlen, pname, fname);
 119     }
 120 }
 121 
 122 void *
 123 dbgsysLoadLibrary(const char * name, char *err_buf, int err_buflen)
 124 {
 125     void *result = LoadLibrary(name);