NetBSD/bin/sh/eval.h

74 lines
3.0 KiB
C
Raw Normal View History

PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking Evaluation of commands goes completely haywire if a file containing a break/continue/return command outside its "intended" scope is sourced using a dot command inside its "intended" scope. The main symptom is not exiting from the sourced file when supposed to, leading to evaluation of commands that were not supposed to be evaluated. A secondary symptom is that these extra commands are not evaluated correctly, as some of them are skipped. Some examples are listed in the How-To-Repeat section. According to the POSIX standard, this is how it should work: dot: The shell shall execute commands from the file in the current environment. break: The break utility shall exit from the smallest enclosing for, while, or until loop, [...] continue: The continue utility shall return to the top of the smallest enclosing for, while, or until loop, [...] return: The return utility shall cause the shell to stop executing the current function or dot script. If the shell is not currently executing a function or dot script, the results are unspecified. It is clear that return should return from a sourced file, which it does not do. Whether break and continue should work from the sourced file might be debatable. Because the dot command says "in the current environment", I'd say yes. In any case, it should not fail in weird ways like it does now! The problems occur with return (a) and break/continue (b) because: 1) dotcmd() does not record the function nesting level prior to sourcing the file nor does it touch the loopnest variable, leading to either 2 a) returncmd() being unable to detect that it should not set evalskip to SKIPFUNC but SKIPFILE, or b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK, leading to 3) cmdloop() not detecting that it should skip the rest of the file, due to only checking for SKIPFILE. The result is that cmdloop() keeps executing lines from the file whilst evalskip is set, which is the main symptom. Because evalskip is checked in multiple places in eval.c, the secondary symptom appears. >How-To-Repeat: Run the following script: printf "break\necho break1; echo break2" >break printf "continue\necho continue1; echo continue2" >continue printf "return\necho return1; echo return2" >return while true; do . ./break; done for i in 1 2; do . ./continue; done func() { . ./return } func No output should be produced, but instead this is the result: break1 continue1 continue1 return1 The main symptom is evident from the unexpected output and the secondary one from the fact that there are no lines with '2' in them. >Fix: Here is patch to src/bin/sh to fix the above problems. It keeps track of the function nesting level at the beginning of a dot command to enable the return command to work properly. I also changed the undefined-by-standard functionality of the return command when it's not in a dot command or function from (indirectly) exiting the shell to being silently ignored. This was done because the previous way has at least one bug: the shell exits without asking for confirmation when there are stopped jobs. Because I read the standard to mean that break and continue should have an effect outside the sourced file, that's how I implemented it. For what it's worth, this also seems to be what bash does. Also laziness, because this way required no changes to loopnesting tracking. If this is not wanted, it might make sense to move the nesting tracking to the inputfile stack. The patch also does some clean-up to reduce the amount of global variables by moving the dotcmd() and the find_dot_file() functions from main.c to eval.c and making in_function() a proper function.
2014-05-31 18:42:18 +04:00
/* $NetBSD: eval.h,v 1.16 2014/05/31 14:42:18 christos Exp $ */
1995-03-21 12:01:59 +03:00
1993-03-21 12:45:37 +03:00
/*-
1994-05-11 21:09:42 +04:00
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* This code is derived from software contributed to Berkeley by
* Kenneth Almquist.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)eval.h 8.2 (Berkeley) 5/4/95
1993-03-21 12:45:37 +03:00
*/
2008-02-15 20:26:06 +03:00
extern const char *commandname; /* currently executing command */
1993-03-21 12:45:37 +03:00
extern int exitstatus; /* exit status of last command */
extern int back_exitstatus; /* exit status of backquoted command */
1993-03-21 12:45:37 +03:00
extern struct strlist *cmdenviron; /* environment for builtin command */
struct backcmd { /* result of evalbackcmd */
int fd; /* file descriptor to read from */
char *buf; /* buffer */
int nleft; /* number of chars in buffer */
struct job *jp; /* job structure for command */
};
void evalstring(char *, int);
1993-03-21 12:45:37 +03:00
union node; /* BLETCH for ansi C */
void evaltree(union node *, int);
void evalbackcmd(union node *, struct backcmd *);
1993-03-21 12:45:37 +03:00
/* in_function returns nonzero if we are currently evaluating a function */
PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking Evaluation of commands goes completely haywire if a file containing a break/continue/return command outside its "intended" scope is sourced using a dot command inside its "intended" scope. The main symptom is not exiting from the sourced file when supposed to, leading to evaluation of commands that were not supposed to be evaluated. A secondary symptom is that these extra commands are not evaluated correctly, as some of them are skipped. Some examples are listed in the How-To-Repeat section. According to the POSIX standard, this is how it should work: dot: The shell shall execute commands from the file in the current environment. break: The break utility shall exit from the smallest enclosing for, while, or until loop, [...] continue: The continue utility shall return to the top of the smallest enclosing for, while, or until loop, [...] return: The return utility shall cause the shell to stop executing the current function or dot script. If the shell is not currently executing a function or dot script, the results are unspecified. It is clear that return should return from a sourced file, which it does not do. Whether break and continue should work from the sourced file might be debatable. Because the dot command says "in the current environment", I'd say yes. In any case, it should not fail in weird ways like it does now! The problems occur with return (a) and break/continue (b) because: 1) dotcmd() does not record the function nesting level prior to sourcing the file nor does it touch the loopnest variable, leading to either 2 a) returncmd() being unable to detect that it should not set evalskip to SKIPFUNC but SKIPFILE, or b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK, leading to 3) cmdloop() not detecting that it should skip the rest of the file, due to only checking for SKIPFILE. The result is that cmdloop() keeps executing lines from the file whilst evalskip is set, which is the main symptom. Because evalskip is checked in multiple places in eval.c, the secondary symptom appears. >How-To-Repeat: Run the following script: printf "break\necho break1; echo break2" >break printf "continue\necho continue1; echo continue2" >continue printf "return\necho return1; echo return2" >return while true; do . ./break; done for i in 1 2; do . ./continue; done func() { . ./return } func No output should be produced, but instead this is the result: break1 continue1 continue1 return1 The main symptom is evident from the unexpected output and the secondary one from the fact that there are no lines with '2' in them. >Fix: Here is patch to src/bin/sh to fix the above problems. It keeps track of the function nesting level at the beginning of a dot command to enable the return command to work properly. I also changed the undefined-by-standard functionality of the return command when it's not in a dot command or function from (indirectly) exiting the shell to being silently ignored. This was done because the previous way has at least one bug: the shell exits without asking for confirmation when there are stopped jobs. Because I read the standard to mean that break and continue should have an effect outside the sourced file, that's how I implemented it. For what it's worth, this also seems to be what bash does. Also laziness, because this way required no changes to loopnesting tracking. If this is not wanted, it might make sense to move the nesting tracking to the inputfile stack. The patch also does some clean-up to reduce the amount of global variables by moving the dotcmd() and the find_dot_file() functions from main.c to eval.c and making in_function() a proper function.
2014-05-31 18:42:18 +04:00
int in_function(void); /* return non-zero, if evaluating a function */
/* reasons for skipping commands (see comment on breakcmd routine) */
PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking Evaluation of commands goes completely haywire if a file containing a break/continue/return command outside its "intended" scope is sourced using a dot command inside its "intended" scope. The main symptom is not exiting from the sourced file when supposed to, leading to evaluation of commands that were not supposed to be evaluated. A secondary symptom is that these extra commands are not evaluated correctly, as some of them are skipped. Some examples are listed in the How-To-Repeat section. According to the POSIX standard, this is how it should work: dot: The shell shall execute commands from the file in the current environment. break: The break utility shall exit from the smallest enclosing for, while, or until loop, [...] continue: The continue utility shall return to the top of the smallest enclosing for, while, or until loop, [...] return: The return utility shall cause the shell to stop executing the current function or dot script. If the shell is not currently executing a function or dot script, the results are unspecified. It is clear that return should return from a sourced file, which it does not do. Whether break and continue should work from the sourced file might be debatable. Because the dot command says "in the current environment", I'd say yes. In any case, it should not fail in weird ways like it does now! The problems occur with return (a) and break/continue (b) because: 1) dotcmd() does not record the function nesting level prior to sourcing the file nor does it touch the loopnest variable, leading to either 2 a) returncmd() being unable to detect that it should not set evalskip to SKIPFUNC but SKIPFILE, or b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK, leading to 3) cmdloop() not detecting that it should skip the rest of the file, due to only checking for SKIPFILE. The result is that cmdloop() keeps executing lines from the file whilst evalskip is set, which is the main symptom. Because evalskip is checked in multiple places in eval.c, the secondary symptom appears. >How-To-Repeat: Run the following script: printf "break\necho break1; echo break2" >break printf "continue\necho continue1; echo continue2" >continue printf "return\necho return1; echo return2" >return while true; do . ./break; done for i in 1 2; do . ./continue; done func() { . ./return } func No output should be produced, but instead this is the result: break1 continue1 continue1 return1 The main symptom is evident from the unexpected output and the secondary one from the fact that there are no lines with '2' in them. >Fix: Here is patch to src/bin/sh to fix the above problems. It keeps track of the function nesting level at the beginning of a dot command to enable the return command to work properly. I also changed the undefined-by-standard functionality of the return command when it's not in a dot command or function from (indirectly) exiting the shell to being silently ignored. This was done because the previous way has at least one bug: the shell exits without asking for confirmation when there are stopped jobs. Because I read the standard to mean that break and continue should have an effect outside the sourced file, that's how I implemented it. For what it's worth, this also seems to be what bash does. Also laziness, because this way required no changes to loopnesting tracking. If this is not wanted, it might make sense to move the nesting tracking to the inputfile stack. The patch also does some clean-up to reduce the amount of global variables by moving the dotcmd() and the find_dot_file() functions from main.c to eval.c and making in_function() a proper function.
2014-05-31 18:42:18 +04:00
enum skipstate {
SKIPNONE = 0, /* not skipping */
SKIPBREAK, /* break */
SKIPCONT, /* continue */
SKIPFUNC, /* return in a function */
SKIPFILE /* return in a dot command */
};
enum skipstate current_skipstate(void);
void stop_skipping(void); /* reset internal skipping state to SKIPNONE */
/*
* Only for use by reset() in init.c!
*/
void reset_eval(void);