When evalcommand invokes a command that modifies parsefile and
then bails out without popping the file, we need to ensure the
input file is restored so that the shell can continue to execute.

Reported-by: Martijn Dekker <mart...@inlv.org>
Signed-off-by: Herbert Xu <herb...@gondor.apana.org.au>

diff --git a/src/eval.c b/src/eval.c
index 7498f9d..e5749ef 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -694,6 +694,7 @@ evalcommand(union node *cmd, int flags)
 #endif
 {
        struct localvar_list *localvar_stop;
+       struct parsefile *file_stop;
        struct redirtab *redir_stop;
        struct stackmark smark;
        union node *argp;
@@ -722,6 +723,7 @@ evalcommand(union node *cmd, int flags)
        TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
        setstackmark(&smark);
        localvar_stop = pushlocalvars();
+       file_stop = parsefile;
        back_exitstatus = 0;
 
        cmdentry.cmdtype = CMDBUILTIN;
@@ -895,6 +897,7 @@ out:
        if (cmd->ncmd.redirect)
                popredir(execcmd);
        unwindredir(redir_stop);
+       unwindfiles(file_stop);
        unwindlocalvars(localvar_stop);
        if (lastarg)
                /* dsl: I think this is intended to be used to support
diff --git a/src/input.c b/src/input.c
index e53423c..ae0c4c8 100644
--- a/src/input.c
+++ b/src/input.c
@@ -479,6 +479,13 @@ popfile(void)
 }
 
 
+void unwindfiles(struct parsefile *stop)
+{
+       while (parsefile != stop)
+               popfile();
+}
+
+
 /*
  * Return to top level.
  */
@@ -486,8 +493,7 @@ popfile(void)
 void
 popallfiles(void)
 {
-       while (parsefile != &basepf)
-               popfile();
+       unwindfiles(&basepf);
 }
 
 
diff --git a/src/input.h b/src/input.h
index ec97c1d..a9c0517 100644
--- a/src/input.h
+++ b/src/input.h
@@ -97,5 +97,6 @@ void popstring(void);
 int setinputfile(const char *, int);
 void setinputstring(char *);
 void popfile(void);
+void unwindfiles(struct parsefile *);
 void popallfiles(void);
 void closescript(void);
-- 
Email: Herbert Xu <herb...@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to