summary refs log tree commit diff
path: root/src/eval.c (follow)
Commit message (Collapse)AuthorAge
* eval: Do not cache value of eflag in evaltreeHerbert Xu2021-06-04
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Patrick Brünn <P.Bruenn@beckhoff.com> wrote: > > Since we are migrating to Debian bullseye, we discovered a new behavior > with our scripts, which look like this: >>#!/bin/sh >>cleanup() { >> set +e^M >> rmdir "" >>} >>set -eu >>trap 'cleanup' EXIT INT TERM >>echo 'Hello world!' > > With old dash v0.5.10.2 this script would return 0 as we expected it. > But since commit 62cf6955f8abe875752d7163f6f3adbc7e49ebae it returns > the last exit code of our cleanup function. > Reverting that commit gives a merge conflict, but it seems to fix _our_ > problem. As that topic appears too complex to us I want to ask the > experts here: > > Is this change in behavior intended, by dash? > > Our workaround at the moment would be: >>trap 'cleanup || true' EXIT INT TERM Thanks for the report. This is actually a fairly old bug with set -e that's just been exposed by the exit status change. What's really happening is that cleanup itself is triggering a set -e exit incorrectly because evaltree cached the value of eflag prior to the function call. This patch should fix the problem. Reported-by: Patrick Brünn <P.Bruenn@beckhoff.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Tested-by: Patrick Brünn <P.Bruenn@beckhoff.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* redir: Clear saved redirections in subshellHerbert Xu2020-04-29
| | | | | | | | | | | | | | | | | | | | | When we enter a subshell we need to drop the saved redirections as otherwise a subsequent unwindredir could produce incorrect results. This patch does this by simply clearing redirlist. While we could actually free the memory underneath for subshells it isn't really worth the trouble for now. In order to ensure that this is done in every place where we enter a subshell, this patch adds a new mkinit hook called forkreset. The calls closescript, clear_traps and reset_handler are also added to the forkreset hook. This fixes a bug where the first two functions weren't called if we enter a subshell without forking. Reported-by: Harald van Dijk <harald@gigawatt.nl> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Reset handler when entering a subshellHerbert Xu2019-03-28
| | | | | | | | | | As it is a subshell can execute code that is only meant for the parent shell when it executes a longjmp that is caught by something like evalcommand. This patch fixes it by resetting the handler when entering a subshell. Reported-by: Martijn Dekker <martijn@inlv.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* shell: Fix clang warnings about "string plus integer"Antonio Ospite2019-02-25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Building with clang results in some warnings about integer values being added to strings: ----------------------------------------------------------------------- eval.c:1138:13: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int] p = " %s" + (1 - sep); ~~~~~~^~~~~~~~~~~ eval.c:1138:13: note: use array indexing to silence this warning p = " %s" + (1 - sep); ^ & [ ] 1 warning generated. ... jobs.c:1424:16: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int] str = "\"}" + !(quoted & 1); ~~~~~~^~~~~~~~~~~~~~~ jobs.c:1424:16: note: use array indexing to silence this warning str = "\"}" + !(quoted & 1); ^ & [ ] 1 warning generated. ----------------------------------------------------------------------- While the code itself is fine and the warnings are indeed harmless, fixing them also makes the semantic more explicit: what it is actually being increased is the address which points to the start of the string in order to skip the initial character when some conditions are met. Signed-off-by: Antonio Ospite <ao2@ao2.it> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Only restore exit status on exit/returnHerbert Xu2019-02-25
| | | | | | | | | | We unconditionally restore the saved status in exitreset, which is incorrect as we only want to do it for exitcmd and returncmd. This patch fixes the problem by introducing EXEND. Reported-by: Martijn Dekker <martijn@inlv.org> Fixes: da30b4b78769 ("[BUILTIN] Exit without arguments in a trap...") Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: avoid leaking memory associated with redirectionsHerbert Xu2019-02-25
| | | | | | | | | | | | | | | | | | | | | The following constructs result in ever-increasing memory usage: while true; do { true; } </dev/null; done while true; do ( true; ) </dev/null; done For comparison, bash displays static memory usage in both cases. This issue was reported for BusyBox ash which is derived from dash: https://bugs.busybox.net/show_bug.cgi?id=7748 Signed-off-by: Ron Yorston <rmy@frippery.org> I have simplified evaltree so that it simply sets the stack mark unconditionally. This allows us to remove the stack marks in the functions called by evaltree. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Use sh_warnx instead of warnxHerbert Xu2018-12-14
| | | | | | | | | | This patch fixes a typo in evalbltin where warnx was used instead of sh_warnx. Reported-by: Antonio Ospite <ao2@ao2.it> Fixes: 8e43729547b5 ("eval: Report I/O error on stdout") Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Use the correct expansion mode for fd redirectionHerbert Xu2018-12-14
| | | | | | | | | | | | | | | It has been reported that echo test >&$EMPTY_VARIABLE causes dash to segfault. This is a symptom of the bigger problem that dash tries to perform pathname expansion as well as field splitting on the word after >& and <&. This is wrong and this patch fixes it to use the same expansions as done on a normal redirection. Reported-by: Andrej Shadura <andrew.shadura@collabora.co.uk> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Silence compiler warning about missing parenthesesAntonio Ospite2018-12-14
| | | | | | | | | | | | | | | | | | | | Gcc gives a warning about some missing parentheses: ----------------------------------------------------------------------- eval.c: In function ‘evaltree’: eval.c:282:15: warning: logical not is only applied to the left hand side of comparison [-Wlogical-not-parentheses] if (!status == isor || evalskip) ^~ eval.c:282:7: note: add parentheses around left hand side expression to silence this warning if (!status == isor || evalskip) ^~~~~~~ ( ) ----------------------------------------------------------------------- Add the parentheses to silence the warning. Signed-off-by: Antonio Ospite <ao2@ao2.it> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: make traps work when "set -e" is enabledAntonio Ospite2018-12-14
| | | | | | | | | | | | | | | | | | | | | | | | | | When "set -e" is enabled traps are not always executed, in particular the EXIT trap is not executed when the shell exits on an unhandled error. Consider the following test script: #!/bin/dash set -e trap 'ret=$?; echo "EXIT: $ret"' EXIT trap 'exit 2' HUP INT QUIT PIPE TERM read variable By pressing Ctrl-C one would expect the EXIT trap to be called, as it is the case with other shells (bash, zsh), but dash does not do it. By calling dotrap() before jumping to the exit path when checkexit is not zero, dash behaves like other shells. Signed-off-by: Antonio Ospite <ao2@ao2.it> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Report I/O error on stdoutGerrit Pape2018-11-19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | ENOSPC as a result of an echo builting failing gives no diagnostic. Just as other shells, dash sets $? to 1, but aside from terminating the script, this does not inform the user what the problem is: zsh: % echo foo > /dev/full echo: write error: no space left on device bash: $ echo foo > /dev/full bash: echo: write error: No space left on device dash: $ echo foo > /dev/full [nothing] Print an error to stderr like the other shells. Suggested by Roger Leigh. Signed-off-by: Gerrit Pape <pape@smarden.org> [reworded the patch description with information from the bug] Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk> Bug-Debian: http://bugs.debian.org/690473 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Always set localvar_stopHerbert Xu2018-08-29
| | | | | | | | | | The variable localvar_stop is set iff vlocal is true. gcc doesn't get this so we get a spurious warning. This patch fixes this by always calling pushlocalvars with vlocal and making it only actually do the push if vlocal is non-zero. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Add vfork supportHerbert Xu2018-05-28
| | | | | | This patch adds basic vfork support for the case of a simple command. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Replace with listsetvar with mklocal/setvareqHerbert Xu2018-05-28
| | | | | | | | | | | | This patch replaces listsetvar with mklocal/setvareq. As we now determine special built-in status prior to variable assignment, we no longer have to do a second pass listsetvar. Instead we will call setvareq directly instead of mklocal when necessary. In order to do this mklocal can now take a flag in order to mark a variable for export. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Fail immediately with redirections errors for simple commandHerbert Xu2018-05-28
| | | | | | | | Previously, dash would continue to perform variable expansions even if a redirection error occured. This patch changes it so that it fails immediately. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Add assignment built-in support againHerbert Xu2018-05-28
| | | | | | | | | | | This patch adds assignment built-in support that used to exist in dash prior to 0.3.8-15. This is because it will soon be part of POSIX, and the semantics are now much better defined. Recognition is done at execution time, so even "command -- export" or "var=export; command $var" should work. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* mkinit: Split reset into exitreset and resetHerbert Xu2018-05-28
| | | | | | | | | | | | | Previously reset was called after exitshell. This was changed so that it was called before exitshell because certain state needed to be reset in order for the EXIT trap to work. However, this caused issues because certain other states (such as local variables) should not be reset. This patch fixes this by creating a new function exitreset that is called prior to exitshell and moving reset back to its original location. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* jobs - Do not block when waiting on SIGCHLDHerbert Xu2018-05-09
| | | | | | | | | | | | | | | Because of the nature of SIGCHLD, the process may have already been waited on and therefore we must be prepared for the case that wait may block. So ensure that it doesn't by using WNOHANG. Furthermore, multiple jobs may have exited when gotsigchld is set. Therefore we need to wait until there are no zombies left. Lastly, waitforjob needs to be called with interrupts off and the original patch broke that. Fixes: 03876c0743a5 ("eval: Reap zombies after built-in...") Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Variable assignments on functions are no longer persistentHerbert Xu2018-04-19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Dirk Fieldhouse <fieldhouse@gmx.net> wrote: > > In POSIX.1-2017 ("simultaneously IEEE Std 1003.1™-2017 and The Open > Group Technical Standard Base Specifications, Issue 7") > <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09>, > we read under '2.9.1 Simple Commands' > > "Variable assignments shall be performed as follows: > ... > - If the command name is a standard utility implemented as a function > (see XBD Utility), the effect of variable assignments shall be as if the > utility was not implemented as a function. > ... > - If the command name is a function that is not a standard utility > implemented as a function, variable assignments shall affect the current > execution environment during the execution of the function. It is > unspecified: > > * Whether or not the variable assignments persist after the > completion of the function > > * Whether or not the variables gain the export attribute during > the execution of the function > > * Whether or not export attributes gained as a result of the > variable assignments persist after the completion of the function (if > variable assignments persist after the completion of the function)" POSIX used to require the current dash behaviour. However, you're right that this is no longer the case. This patch will remove the persistence of the variable assignment. I have considered the exporting the variables during the function execution but have decided against it because: 1) It makes the code bigger. 2) dash has never done this in the past. 3) You cannot use this portably anyway. Reported-by: Dirk Fieldhouse <fieldhouse@gmx.net> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Restore input files in evalcommandHerbert Xu2018-04-19
| | | | | | | | | 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 <martijn@inlv.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Reap zombies after built-in commands and functionsHerbert Xu2018-04-19
| | | | | | | | | | | | | Currently dash does not reap dead children after built-in commands or functions. This means that if you construct a loop consisting of solely built-in commands and functions, then zombies can hang around indefinitely. This patch fixes this by reaping when necessary after each built-in command and function. Reported-by: Denys Vlasenko <vda.linux@googlemail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Return status in eval functionsHerbert Xu2016-09-02
| | | | | | | | | | The exit status is currently clobbered too early for case statements and loops. This patch fixes it by making the eval functions return the current exit status and setting them in one place -- evaltree. Harald van Dijk pointed out a number of bugs in the original patch. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* eval: Fix exit status when calling eval/dot with no commandsHarald van Dijk2016-06-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On 17/11/2015 03:18, Gioele Barabucci wrote: > Hello, > > a bug has been filed in the Debian BTS about dash not resetting the exit > status after sourcing an empty file with the dot command. [1] > > The following test echoes "OK" with bash and "fail" with dash > > #!/bin/sh > > echo > ./empty > false > > . ./empty && echo "OK" || echo "fail" > > A similar bug in dash has been discussed and addressed in 2011 [2], but > it looks like the solution has been only partial. > > The version of dash I tested is the current git master branch, commit > 2e58422. > > [1] https://bugs.debian.org/777262 > [2] http://article.gmane.org/gmane.comp.shells.dash/531 The bug described there was about empty files. While the fix has been applied and does make dash handle empty files properly, your test doesn't use an empty file, it uses a file containing a single blank line. Unfortunately, the single blank line gets parsed by dash as a null command, null commands don't (and shouldn't) reset the exit status, and the fix you link to doesn't handle this because it sees a command has been executed and saves the exit status after executing that command as the exit status to be used by ".". I think the easiest way to fix this is to prevent null commands from affecting status in cmdloop, as attached. An alternative could be to change the outer if condition to exclude n == NULL, but I didn't do that because the change of job_warning and clearing of numeof make sense to me even for null commands. Besides, when debug tracing is enabled, null commands have a visible effect that should remain. Note that this fixes the problem with . but the same problem can be present in other locations. For example, false eval " " && echo OK || echo Fail used to print Fail, and needed the same modification in the evalstring function to make that print OK (included in the attached patch). There may be other similar bugs lurking. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Return without arguments in a trap should use status outside trapsHerbert Xu2014-10-06
| | | | | | | | | | | | | POSIX now requires that return without arguments in a trap should return the last command status prior to executing traps. This patch implements this behaviour. Incidentally this also changes the behaviour of return without arguments in a loop conditional to use the last exit status in the body as opposed to the last command in the conditional when there is one. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Allow return in loop conditional to set exit statusHerbert Xu2014-10-06
| | | | | | | | | | | | | | | | | | https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=332954 When return is used in a loop conditional the exit status will be lost because we always set the exit status at the end of the loop to that of the last command executed in the body. This is counterintuitive and contrary to what most other shells do. This patch fixes this by always preserving the exit status of return when it is used in a loop conditional. The patch was originally written by Gerrit Pape <pape@smarden.org>. Reported-by: Stephane Chazelas <stephane_chazelas@yahoo.fr> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Move common skipcount logic into skiploopHerbert Xu2014-10-06
| | | | | | | | The functions evalloop and evalfor share the logic on checking and updating skipcount. This patch moves that into the helper function skiploop. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Do not allow break to break across function callsHerbert Xu2014-10-06
| | | | | | | | | | | As it is if you do a multi-level break inside a function it'll actually include loops outside of the function call. This is counterintuitive. This patch changes this by saving and resetting loopnest when entering a function. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Exit without arguments in a trap should use status outside trapsHerbert Xu2014-10-06
| | | | | | | | POSIX now requires that exit without arguments in a trap should return the last command status prior to executing traps. This patch implements this behaviour. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Do not clobber exitstatus in evalcommandHerbert Xu2014-10-03
| | | | | | | | | | All originators of EXERROR have been setting the exitstatus for a while now. So it is no longer appropriate to set it explicitly in evalcommand. In fact doing so may cause the original exitstatus to be lost. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [ERROR] Set exitstatus in onintHerbert Xu2014-10-02
| | | | | | | | | | | | Currently the exit status when we receive SIGINT is set in evalcommand which means that it doesn't always get set. For example, if you press CTRL-C at the prompt of an interactive dash, the exit status is not set to 130 as it is in many other Bourne shells. This patch fixes this by moving the setting of the exit status into onint which also simplifies evalcommand. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [TRAP] Make sure evalskip is zero before running trapsHerbert Xu2014-10-02
| | | | | | | | | | | | | | | | | | | | | | | As it is if dotrap is called with evalskip set to a nonzero value, it'll try to execute any set traps. The result is that the first command in the first set trap will be executed while the rest of the trap will be silently ignored due to evalskip. This is highly counterintuitive, even though both bash and ksh exhibit a similar behaviour. This patch fixes it by skipping trap processing if evalskip is set on entry. It also adds a dotrap call to the top of evaltree to ensure that while continue; do continue; done has a chance of running traps. Finally the pendingsigs check is moved into dotrap for compactness. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Fix use-after-free in dotrap/evalstringHerbert Xu2014-10-02
| | | | | | | | | | | The function dotrap calls evalstring using the stored trap string. If evalstring then unsets that exact trap string then we will end up using freed memory. This patch fixes it by making evalstring always duplicate the string before using it. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Merge SKIPFUNC/SKIPFILE and only clear SKIPFUNC when leaving dotcmdHerbert Xu2011-07-09
| | | | | | | | | | | | | | | | Currently upon leaving a dotcmd the evalskip state is reset so if a continue/break statement is used within a dot script it would have no effect outside of the dot script. This is inconsistent with other shells. This patch is based on one by Jilles Tjoelker and only clears SKIPFUNC when leaving a dot script. As a result continue/break will remain in effect. It also merges SKIPFUNC/SKIPFILE as they have no practical difference. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Avoid using undefined handlerJim Meyering2011-07-08
| | | | | | | | | | * src/eval.c (evalbltin, evalfun): Set savehandler before calling setjmp with the possible "goto *done", where savehandler is used. Otherwise, clang warns that "Assigned value is garbage or undefined" at the point where "savehandler" is used on the RHS. Signed-off-by: Jim Meyering <meyering@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [SHELL] Optimize dash -c "command" to avoid a forkHerbert Xu2011-07-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On Sun, Apr 10, 2011 at 07:36:49AM +0000, Jonathan Nieder wrote: > From: Jilles Tjoelker <jilles@stack.nl> > Date: Sat, 13 Jun 2009 16:17:45 -0500 > > This change only affects strings passed to -c, when the -s option is > not used. > > Use the EV_EXIT flag to inform the eval machinery that the string > being passed is the entirety of input. This way, a fork may be > omitted in many special cases. > > If there are empty lines after the last command, the evalcmd will not > see the end early enough and forks will not be omitted. The same thing > seems to happen in bash. > > Example: > sh -c 'ps lT' > No longer shows a shell process waiting for ps to finish. > > [jn: ported from FreeBSD SVN r194128. Bugs are mine.] > > Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Instead of detecting EOF using the input layer, I'm going to use the parser instead. In either case, we always have to read ahead in order to complete the parsing of the previous node. Therefore we always know whether there is more to come, except in the case where we see a newline/semicolon or similar. For the purposes of sh -c, this should be sufficient. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Remove unused EV_BACKCMD flagJonathan Nieder2011-07-07
| | | | | | | | | | | | | | | | | | | | | The original ash defered forking commands in backquotes so builtins could be run in the same context as the shell. This behavior was controlled using the EV_BACKCMD to evaltree. Unfortunately, as Matthias Scheler noticed in 1999 (NetBSD PR/7814), the result was counterintuitive; for example, echo "`cd /`" would change the cwd. So ash 0.3.5 left out that optimization. The EV_BACKCMD codepath stayed around, unused. Some time between ash 0.3.5-11 and ash 0.3.8-37, Debian ash omitted the EV_BACKCMD pathway by guarding it with #ifdef notyet. In dash 0.5.1 and later, the commented code is no more. Let's finish the job and remove the last vestiges. If someone wants to work on omitting the fork in backcmd, the remaining hints are not going to be very helpful, anyway. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [SHELL] Improve LINENO supportHarald van Dijk2011-03-15
| | | | | | | | | | | | This patch improves LINENO support by storing line numbers in the parse tree, for commands as well as for function definitions. It makes LINENO behaves properly when calling functions, and has the added benefit of improved line numbers in error messages when the last-parsed command is not the last-executed one. It removes the earlier LINENO support, and instead sets LINENO from evaltree when a command is executed Signed-off-by: Harald van Dijk <harald@gigawatt.nl> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Let funcnode refer to a function definition, not its first commandHarald van Dijk2011-03-15
| | | | | | | | | | | | It is not unrelated: I changed the meaning of struct funcnode's field n to refer to the function definition, rather than the list of the function's commands, because I needed to refer to the function definition node from evalfun, which only gets passed a funcnode. But it is something that could be applied independently (without being useful by itself), so I've attached it as a separate patch for easier review. Signed-off-by: Harald van Dijk <harald@gigawatt.nl> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EXPAND] Free IFS state in evalbackcmdHerbert Xu2010-11-28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On Sun, Nov 07, 2010 at 04:04:20PM -0600, Jonathan Nieder wrote: > > Herbert Xu wrote: > > > commit f42e443bb511ed3224f09b4fcf0772438ebdbbfa > > Author: Herbert Xu <herbert@gondor.apana.org.au> > > Date: Wed Sep 8 20:07:26 2010 +0800 > > > > [EXPAND] Fix ifsfirst/ifslastp leak > > Another puzzle bisecting to f42e443bb. This one comes from the > grub-mkconfig script: > > $ sh -c 'datadir=/usr/share; pkgdatadir=${datadir}/`cat`' 2>&1 | cat -A > cat: M-^\^M^F^HM-4^M^F^HM-(^M^F^H: No such file or directory$ > cat: M-(^M^F^H: No such file or directory$ > > Still reproducible with 016b529. I'll try to find time to look into > it, but thought you might like to know nevertheless. This is the symptom of another leak. In this case evalbackcmd occurs in the middle of an expansion (as it should) but the forked child never clears the previous IFS state. This patch adds the missing ifsfree call. This wasn't as much of a problem as the previously discovered leaks since all it means is that the child gets to carry around the parent's expansion state and the child is usually short-lived. Reported-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Use EXEXIT in place of EXEXECJonathan Nieder2010-11-28
| | | | | | | | | | | | | | | | | | | | | | The intended semantics of EXEXEC are identical to EXEXIT, so simplify by using EXEXIT directly. Functional change: in edge cases (exec within a trap handler), this causes the exit status from exec not to be clobbered. For example, without this patch: $ sh -c 'trap "exec nonexistent" EXIT'; echo $? exec: 1: nonexistent: not found 0 And with it: $ sh -c 'trap "exec nonexistent" EXIT'; echo $? exec: 1: nonexistent: not found 127 Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [ERROR] Allow the originator of EXERROR to set the exit statusHerbert Xu2010-11-28
| | | | | | | | Some errors have exit status values specified by POSIX and it is therefore desirable to be able to set the exit status at the EXERROR source rather than in main.c. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Fixed trap/return regression due to SKIPEVAL removalHerbert Xu2010-11-28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On Wed, Aug 11, 2010 at 08:06:16AM +0000, Guido Berhoerster wrote: > > with the latest git version of dash trap actions are not > evaluated in the context of a function. > > The following script demonstrates the bug: > ----8<---- > read_timeout () { > saved_traps="$(trap)" > trap 'printf "timed out\n"; eval "${saved_traps}"; return' TERM > ( sleep $1; kill -TERM $$ ) >/dev/null 2>&1 & > timer_pid=$! > read $2 > kill $timer_pid 2>/dev/null > } > > read_timeout 5 value > printf "read \"%s\"\n" "${value:=default}" > > ---->8---- > The return statement in the trap inside the read_timeout function > does not return from the function but rather exits the script. > > With dash 0.5.5.1 it works as expected. This bug was caused by the SKIPEVAL removal. When the SKIPEVAL hack was added to improve set -e support in traps, dotrap was changed to return whether set -e was detected. After the removal of SKIPEVAL, set -e is now handled through exraise. However, dotrap still returned a value which is now incorrectly used to trigger an exraise. This patch removes the vestigial link between dotrap and exraise. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [BUILTIN] Fix EXEXEC status clobberingHerbert Xu2010-10-07
| | | | | | | | | | | evalcommand always clobbers the exit status in case of an EXEXEC which means that exec always fails with exit status 2 regardless of what it actually returns. This patch adds the missing check for EXEXEC so that the correct exit status is preserved. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Check exit for eval NSUBSHELLGerrit Pape2010-07-06
| | | | | | | | | | | | | | | Example: $ dash -c 'set -e; (false); echo here' here With this commit, dash exits 1 before echo. The bug was reported by Stefan Fritsch through http://bugs.debian.org/514863 Signed-off-by: Gerrit Pape <pape@smarden.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Removed dead code for eval NPIPEHerbert Xu2010-07-06
| | | | | | The notyet code is identical to the current code. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Don't clear eflag in evalbackcmdGerrit Pape2010-06-28
| | | | | | | | | | | | | | | | | | | | | | According to http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_12 "A subshell environment shall be created as a duplicate of the shell environment, except that signal traps set by that shell environment shall be set to the default values." Currently the eflag is cleared when forking a subshell, e.g. $ dash -c 'set -e ; z=$(false;echo foo) ; echo $z' foo With this commit the eflag is preserved for subshells, and dash exits 1 before echo. The problem was reported by Vincent Lefevre through http://bugs.debian.org/514863 Signed-off-by: Gerrit Pape <pape@smarden.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [EVAL] Force fork if any trap is set, not just on EXITJilles Tjoelker2010-05-27
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | In some cases the shell executes a subshell or an external command in the current process. This is not done if a trap on EXIT has been set, so that that trap can execute after the subshell or external command has finished. Extend that check to all traps. (A trap is "set" if a non-empty command string has been attached to it.) Improve encapsulation by exporting an accessor function for this and making the trap array static again. This is much like FreeBSD SVN r194127, enhanced to apply to subshells also (see FreeBSD SVN r194774). Example: dash -c '{ trap "echo moo" TERM; sleep 3; }& sleep 1; kill $!;wait' This should print "moo" after 3 seconds. Example: dash -c '{ trap "echo moo" TERM; (sleep 3) }& sleep 1; kill $!;wait' The same. Example: dash -c '{ trap "echo moo" TERM; sleep 3; :; }& sleep 1; kill $!;wait' This works correctly even without this patch. Signed-off-by: Jilles Tjoelker <jilles@stack.nl> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [REDIR] Fix popredir on abnormal exit from built-inHerbert Xu2010-05-27
| | | | | | | | | Just like the poplocalvar problem recently fixed, redirections can also be leaked in case of an abnormal exit. This patch fixes it using the same method as poplocalvar, by storing the previous redirection state and restoring to that point. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [REDIR] Move null redirect checks into callerHerbert Xu2010-05-27
| | | | | | | | | The null redirect checks were added as an optimisation to avoid unnecessary memory allocations. However, we could avoid this completely by simply making the caller avoid making a redirection unless it is not null. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
* [VAR] Do not poplocalvars prematurely on regular utilitiesHerbert Xu2010-05-27
| | | | | | | | | | | The recent cmdenviron removal broke regular utilities by calling poplocalvars too early. This patch fixes that by postponing the poplocalvars for regular utilities until they have completed. In order to ensure that local still works, it is now a special built-in. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>