ash: [EVAL] Pass EV_TESTED into evalcmd

Upstream commit:

    Date:   Tue Aug 11 20:48:15 2009 +1000
    [EVAL] Pass EV_TESTED into evalcmd

    This patch fixes the case where the eval command is used with
    set -e and as part of a construct that should not cause the
    shell to abort, e.g., as part of the condition of an if statement.

    This is achieved by propagating the EV_TESTED flag into the
    evalstring function through evalcmd.  As this alters the prototype
    of evalcmd it is now invoked explicitly by evalbltin.  The built-in
    infrastructure has been changed to accomodate this special case.

    In order to ensure that the EXIT trap is properly executed this
    patch clears evalskip in exitshell.  This wasn't needed before
    because of the broken way evalstring worked where it always clears
    evalskip when called by minusc.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Although, I failed to create a reproducer for this.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2016-10-01 15:10:16 +02:00
parent 08089c7c85
commit 7b3fa1e441

View File

@ -8414,7 +8414,7 @@ static int funcnest; /* depth of function calls */
static int loopnest; /* current loop nesting level */ static int loopnest; /* current loop nesting level */
/* Forward decl way out to parsing code - dotrap needs it */ /* Forward decl way out to parsing code - dotrap needs it */
static int evalstring(char *s, int mask); static int evalstring(char *s, int flags);
/* Called to execute a trap. /* Called to execute a trap.
* Single callsite - at the end of evaltree(). * Single callsite - at the end of evaltree().
@ -8450,7 +8450,7 @@ dotrap(void)
*g = 0; *g = 0;
if (!t) if (!t)
continue; continue;
evalstring(t, SKIPEVAL); evalstring(t, 0);
exitstatus = savestatus; exitstatus = savestatus;
if (evalskip) { if (evalskip) {
TRACE(("dotrap returns %d\n", evalskip)); TRACE(("dotrap returns %d\n", evalskip));
@ -8470,7 +8470,7 @@ static int evalsubshell(union node *, int);
static void expredir(union node *); static void expredir(union node *);
static int evalpipe(union node *, int); static int evalpipe(union node *, int);
static int evalcommand(union node *, int); static int evalcommand(union node *, int);
static int evalbltin(const struct builtincmd *, int, char **); static int evalbltin(const struct builtincmd *, int, char **, int);
static void prehash(union node *); static void prehash(union node *);
/* /*
@ -9177,7 +9177,7 @@ returncmd(int argc UNUSED_PARAM, char **argv)
/* Forward declarations for builtintab[] */ /* Forward declarations for builtintab[] */
static int breakcmd(int, char **) FAST_FUNC; static int breakcmd(int, char **) FAST_FUNC;
static int dotcmd(int, char **) FAST_FUNC; static int dotcmd(int, char **) FAST_FUNC;
static int evalcmd(int, char **) FAST_FUNC; static int evalcmd(int, char **, int) FAST_FUNC;
static int exitcmd(int, char **) FAST_FUNC; static int exitcmd(int, char **) FAST_FUNC;
static int exportcmd(int, char **) FAST_FUNC; static int exportcmd(int, char **) FAST_FUNC;
#if ENABLE_ASH_GETOPTS #if ENABLE_ASH_GETOPTS
@ -9247,7 +9247,7 @@ static const struct builtincmd builtintab[] = {
#if ENABLE_ASH_BUILTIN_ECHO #if ENABLE_ASH_BUILTIN_ECHO
{ BUILTIN_REGULAR "echo" , echocmd }, { BUILTIN_REGULAR "echo" , echocmd },
#endif #endif
{ BUILTIN_SPEC_REG "eval" , evalcmd }, { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/
{ BUILTIN_SPEC_REG "exec" , execcmd }, { BUILTIN_SPEC_REG "exec" , execcmd },
{ BUILTIN_SPEC_REG "exit" , exitcmd }, { BUILTIN_SPEC_REG "exit" , exitcmd },
{ BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, { BUILTIN_SPEC_REG_ASSG "export" , exportcmd },
@ -9567,7 +9567,7 @@ evalcommand(union node *cmd, int flags)
* to reap the zombie and make kill detect that it's gone: */ * to reap the zombie and make kill detect that it's gone: */
dowait(DOWAIT_NONBLOCK, NULL); dowait(DOWAIT_NONBLOCK, NULL);
if (evalbltin(cmdentry.u.cmd, argc, argv)) { if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
int exit_status; int exit_status;
int i = exception_type; int i = exception_type;
if (i == EXEXIT || i == EXEXEC) if (i == EXEXIT || i == EXEXEC)
@ -9612,11 +9612,12 @@ evalcommand(union node *cmd, int flags)
} }
static int static int
evalbltin(const struct builtincmd *cmd, int argc, char **argv) evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags)
{ {
char *volatile savecmdname; char *volatile savecmdname;
struct jmploc *volatile savehandler; struct jmploc *volatile savehandler;
struct jmploc jmploc; struct jmploc jmploc;
int status;
int i; int i;
savecmdname = commandname; savecmdname = commandname;
@ -9628,10 +9629,14 @@ evalbltin(const struct builtincmd *cmd, int argc, char **argv)
commandname = argv[0]; commandname = argv[0];
argptr = argv + 1; argptr = argv + 1;
optptr = NULL; /* initialize nextopt */ optptr = NULL; /* initialize nextopt */
exitstatus = (*cmd->builtin)(argc, argv); if (cmd == EVALCMD)
status = evalcmd(argc, argv, flags);
else
status = (*cmd->builtin)(argc, argv);
flush_stdout_stderr(); flush_stdout_stderr();
status |= ferror(stdout);
exitstatus = status;
cmddone: cmddone:
exitstatus |= ferror(stdout);
clearerr(stdout); clearerr(stdout);
commandname = savecmdname; commandname = savecmdname;
exception_handler = savehandler; exception_handler = savehandler;
@ -12241,7 +12246,7 @@ expandstr(const char *ps)
* Execute a command or commands contained in a string. * Execute a command or commands contained in a string.
*/ */
static int static int
evalstring(char *s, int mask) evalstring(char *s, int flags)
{ {
union node *n; union node *n;
struct stackmark smark; struct stackmark smark;
@ -12255,7 +12260,7 @@ evalstring(char *s, int mask)
while ((n = parsecmd(0)) != NODE_EOF) { while ((n = parsecmd(0)) != NODE_EOF) {
int i; int i;
i = evaltree(n, 0); i = evaltree(n, flags);
if (n) if (n)
status = i; status = i;
popstackmark(&smark); popstackmark(&smark);
@ -12266,7 +12271,6 @@ evalstring(char *s, int mask)
popfile(); popfile();
stunalloc(s); stunalloc(s);
evalskip &= mask;
return status; return status;
} }
@ -12274,7 +12278,7 @@ evalstring(char *s, int mask)
* The eval command. * The eval command.
*/ */
static int FAST_FUNC static int FAST_FUNC
evalcmd(int argc UNUSED_PARAM, char **argv) evalcmd(int argc UNUSED_PARAM, char **argv, int flags)
{ {
char *p; char *p;
char *concat; char *concat;
@ -12294,7 +12298,7 @@ evalcmd(int argc UNUSED_PARAM, char **argv)
STPUTC('\0', concat); STPUTC('\0', concat);
p = grabstackstr(concat); p = grabstackstr(concat);
} }
return evalstring(p, ~SKIPEVAL); return evalstring(p, flags & EV_TESTED);
} }
return 0; return 0;
} }
@ -13112,6 +13116,7 @@ exitshell(void)
p = trap[0]; p = trap[0];
if (p) { if (p) {
trap[0] = NULL; trap[0] = NULL;
evalskip = 0;
evalstring(p, 0); evalstring(p, 0);
free(p); free(p);
} }