handle library paths better (ignore empty path components, etc.)
[musl] / src / ldso / dynlink.c
index f57485d..472e389 100644 (file)
@@ -123,7 +123,7 @@ static void do_relocs(unsigned char *base, size_t *rel, size_t rel_size, size_t
 
 static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dynp)
 {
-       size_t buf[896/sizeof(size_t)];
+       Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
        size_t phsize;
        size_t addr_min=SIZE_MAX, addr_max=0, map_len;
        size_t this_min, this_max;
@@ -137,11 +137,11 @@ static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dy
 
        ssize_t l = read(fd, buf, sizeof buf);
        if (l<sizeof *eh) return 0;
-       eh = (void *)buf;
+       eh = buf;
        phsize = eh->e_phentsize * eh->e_phnum;
        if (phsize + sizeof *eh > l) return 0;
        if (eh->e_phoff + phsize > l) {
-               l = pread(fd, buf+sizeof *eh, phsize, eh->e_phoff);
+               l = pread(fd, buf+1, phsize, eh->e_phoff);
                if (l != phsize) return 0;
                eh->e_phoff = sizeof *eh;
        }
@@ -208,15 +208,17 @@ static void *map_library(int fd, size_t *lenp, unsigned char **basep, size_t *dy
 static int path_open(const char *name, const char *search)
 {
        char buf[2*NAME_MAX+2];
-       const char *s, *z;
+       const char *s=search, *z;
        int l, fd;
-       for (s=search; *s; s+=l+!!z) {
+       for (;;) {
+               while (*s==':') s++;
+               if (!*s) return -1;
                z = strchr(s, ':');
                l = z ? z-s : strlen(s);
                snprintf(buf, sizeof buf, "%.*s/%s", l, s, name);
                if ((fd = open(buf, O_RDONLY))>=0) return fd;
+               s += l;
        }
-       return -1;
 }
 
 static struct dso *load_library(const char *name)