fix (normal, narrow) printf erroneously processing %n after output errors
[musl] / src / misc / wordexp.c
index 8f0d42f..db83a69 100644 (file)
@@ -8,8 +8,15 @@
 #include <sys/wait.h>
 #include <signal.h>
 #include <errno.h>
+#include <fcntl.h>
 #include "pthread_impl.h"
 
+static void reap(pid_t pid)
+{
+       int status;
+       while (waitpid(pid, &status, 0) < 0 && errno == EINTR);
+}
+
 static char *getword(FILE *f)
 {
        char *s = 0;
@@ -23,7 +30,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        size_t np=0;
        char *w, **tmp;
        char *redir = (flags & WRDE_SHOWERR) ? "" : "2>/dev/null";
-       int err = 0, status;
+       int err = 0;
        FILE *f;
        size_t wc = 0;
        char **wv = 0;
@@ -35,7 +42,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
 
        if (flags & WRDE_NOCMD) for (i=0; s[i]; i++) switch (s[i]) {
        case '\\':
-               if (!sq) i++;
+               if (!sq && !s[++i]) return WRDE_SYNTAX;
                break;
        case '\'':
                if (!dq) sq^=1;
@@ -64,6 +71,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
                if (!(sq|dq|np)) return WRDE_BADCHAR;
                break;
        case '$':
+               if (sq) break;
                if (s[i+1]=='(' && s[i+2]=='(') {
                        i += 2;
                        np += 2;
@@ -88,7 +96,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
                we->we_offs = 0;
        }
 
-       if (pipe(p) < 0) goto nospace;
+       if (pipe2(p, O_CLOEXEC) < 0) goto nospace;
        __block_all_sigs(&set);
        pid = fork();
        __restore_sigs(&set);
@@ -98,9 +106,8 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
                goto nospace;
        }
        if (!pid) {
-               dup2(p[1], 1);
-               close(p[0]);
-               close(p[1]);
+               if (p[1] == 1) fcntl(1, F_SETFD, 0);
+               else dup2(p[1], 1);
                execl("/bin/sh", "sh", "-c",
                        "eval \"printf %s\\\\\\\\0 x $1 $2\"",
                        "sh", s, redir, (char *)0);
@@ -112,7 +119,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        if (!f) {
                close(p[0]);
                kill(pid, SIGKILL);
-               waitpid(pid, &status, 0);
+               reap(pid);
                goto nospace;
        }
 
@@ -121,8 +128,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        free(getword(f));
        if (feof(f)) {
                fclose(f);
-               while ((waitpid(pid, &status, 0) < 0 && errno == EINTR)
-                       || !WIFEXITED(status));
+               reap(pid);
                return WRDE_SYNTAX;
        }
 
@@ -139,8 +145,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        if (!feof(f)) err = WRDE_NOSPACE;
 
        fclose(f);
-       while ((waitpid(pid, &status, 0) < 0 && errno == EINTR)
-               || !WIFEXITED(status));
+       reap(pid);
 
        if (!wv) wv = calloc(i+1, sizeof *wv);