fix regression in glob with literal . or .. path component
[musl] / src / regex / glob.c
index 9a70f0b..8567198 100644 (file)
@@ -7,8 +7,6 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <stddef.h>
-#include <unistd.h>
-#include <stdio.h>
 #include "libc.h"
 
 struct match
@@ -53,13 +51,13 @@ static int append(struct match **tail, const char *name, size_t len, int mark)
 static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
 {
        DIR *dir;
-       long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)];
-       struct dirent *de;
+       struct dirent de_buf, *de;
        char pat[strlen(p)+1];
        char *p2;
        size_t l = strlen(d);
        int literal;
-       int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD;
+       int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
+               | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
        int error;
 
        if ((p2 = strchr(p, '/'))) {
@@ -94,7 +92,7 @@ static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(
                closedir(dir);
                return error;
        }
-       while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) {
+       while (!(error = readdir_r(dir, &de_buf, &de)) && de) {
                char namebuf[l+de->d_reclen+2], *name = namebuf;
                if (!literal && fnmatch(p, de->d_name, fnm_flags))
                        continue;
@@ -102,6 +100,12 @@ static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(
                        continue;
                if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
                        continue;
+               /* With GLOB_PERIOD, don't allow matching . or .. unless
+                * fnmatch would match them with FNM_PERIOD rules in effect. */
+               if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
+                   && (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2])
+                   && fnmatch(p, de->d_name, fnm_flags | FNM_PERIOD))
+                       continue;
                if (*d) {
                        memcpy(name, d, l);
                        name[l] = '/';
@@ -117,7 +121,7 @@ static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(
                } else {
                        int mark = 0;
                        if (flags & GLOB_MARK) {
-                               if (de->d_type)
+                               if (de->d_type && !S_ISLNK(de->d_type<<12))
                                        mark = S_ISDIR(de->d_type<<12);
                                else {
                                        struct stat st;
@@ -156,7 +160,7 @@ static int sort(const void *a, const void *b)
        return strcmp(*(const char **)a, *(const char **)b);
 }
 
-int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
+int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
 {
        const char *p=pat, *d;
        struct match head = { .next = NULL }, *tail = &head;
@@ -179,7 +183,9 @@ int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err),
                g->gl_pathv = NULL;
        }
 
-       if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
+       if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
+
+       if (*pat) error = match_in_dir(d, p, flags, errfunc, &tail);
        if (error == GLOB_NOSPACE) {
                freelist(&head);
                return error;