Commit Graph

392 Commits

Author SHA1 Message Date
Denys Vlasenko
1c54552842 ash: fix ifs cleanup on error paths
Patch by Alex Gorinson <algore3698@gmail.com>

function                                             old     new   delta
evalvar                                              477     495     +18
varvalue                                             603     618     +15
subevalvar                                          1557    1572     +15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 48/0)               Total: 48 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-08-02 11:18:11 +02:00
Sören Tempel
fa52ac9781 ash: don't read past end of var in subvareval for bash substitutions
Without this patch, BusyBox handles bash pattern substitutions without
a terminating '/' character incorrectly.

Consider the following shell script:

	_bootstrapver=5.0.211-r0
	_referencesdir="/usr/${_bootstrapver/-*}/Sources"
	echo $_referencesdir

This should output `/usr/5.0.211/Sources`. However, without this patch
it instead outputs `/usr/5.0.211Sources`. This is due to the fact that
BusyBox expects the bash pattern substitutions to always be terminated
with a '/' (at least in this part of subvareval) and thus reads passed
the substitution itself and consumes the '/' character which is part of
the literal string. If there is no '/' after the substitution then
BusyBox might perform an out-of-bounds read under certain circumstances.

When replacing the bash pattern substitution with `${_bootstrapver/-*/}`,
or with this patch applied, ash outputs the correct value.

Signed-off-by: Sören Tempel <soeren@soeren-tempel.net>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-03-01 08:47:43 +01:00
Denys Vlasenko
5acf5e1f87 shell: fix script's comm field if ENABLE_FEATURE_PREFER_APPLETS=y
function                                             old     new   delta
re_execed_comm                                         -      46     +46
main                                                  72      86     +14
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 60/0)               Total: 60 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-10-11 18:44:00 +02:00
Denys Vlasenko
1be73dd9ad shell: fix parsing of $(( (v)++ + NUM ))
function                                             old     new   delta
evaluate_string                                      988    1011     +23

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-26 13:29:25 +02:00
Denys Vlasenko
62e433131b shell: enable more tests which are passing now
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-25 22:35:17 +02:00
Denys Vlasenko
d84a604830 shell: fix arithmentic evaluation of "++7" and such (it is + + 7, i.e. 7)
function                                             old     new   delta
evaluate_string                                      945     988     +43

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-25 22:04:45 +02:00
Denys Vlasenko
64aa86b720 ash: LINENO starts from 0 in -c SCRIPT mode
The var_LINENO3.tests fails for hush: it does start from 0, but does not increment.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-07 18:16:45 +02:00
Denys Vlasenko
d6c9cbc072 ash: fix LINENO in functions
From larger patch by Roberto A. Foglietta <roberto.foglietta@gmail.com>

function                                             old     new   delta
evalfun                                              348     369     +21
ash_main                                            1202    1218     +16
setinputstring                                        65      73      +8
lookupvar                                            116     106     -10
evaltree                                             772     753     -19
evalsubshell                                         192     173     -19
evalfor                                              175     156     -19
evalcase                                             273     254     -19
evalcommand                                         1560    1536     -24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/6 up/down: 45/-110)           Total: -65 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-07 18:01:49 +02:00
Denys Vlasenko
e53c7dbafc hush: fix set -n to act immediately, not just after run_list()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-09-07 02:25:52 +02:00
Denys Vlasenko
c450437a4e shell: update psubst testcases
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-27 04:20:32 +02:00
Denys Vlasenko
b278d82c61 hush: implement $'str' bashism
function                                             old     new   delta
parse_dollar_squote                                    -     441    +441
encode_then_expand_vararg                            359     380     +21
parse_stream                                        2252    2271     +19
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 481/0)             Total: 481 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-26 15:32:46 +02:00
Denys Vlasenko
05c5d745f7 ahell: update testsuite
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-07-25 22:03:16 +02:00
Denys Vlasenko
97c3b5e3ff hush: fix bkslash+newline handling and number validation in ${NN} and ${#NN}
Entering "${1a}" into interactive shell was making it exit.

function                                             old     new   delta
parse_dollar                                         824     958    +134
i_getch_and_eat_bkslash_nl                             -      44     +44
parse_expr                                           917     938     +21
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 199/0)             Total: 199 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-19 15:45:45 +02:00
Denys Vlasenko
1b7a9b68d0 hush: fix handling of \^C and "^C"
function                                             old     new   delta
parse_stream                                        2238    2252     +14
encode_string                                        243     256     +13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 27/0)               Total: 27 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2021-06-15 16:46:30 +02:00
Denys Vlasenko
cad20ced86 typo fix
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-25 19:08:16 +01:00
Denys Vlasenko
1237d627e9 hush: fix this case: echo "SCRIPT" | hush
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-25 19:01:49 +01:00
Denys Vlasenko
a7b52d26c4 hush: fix fallout from tweaking ${var:START:LEN} code
function                                             old     new   delta
expand_one_var                                      2344    2353      +9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-12-23 12:38:03 +01:00
Denys Vlasenko
64981b4c8e hush: document bugs in [[ =~ ]] comparison
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-11-01 18:47:24 +01:00
Denys Vlasenko
b259e97d47 hush: add tests for unquoted < > in [[ ]]
They pass.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-10-31 03:51:37 +01:00
Denys Vlasenko
d2241f5902 shell: better support of [[ ]] bashism
Still rather rudimentary for ash

function                                             old     new   delta
binop                                                433     589    +156
check_operator                                        65     101     +36
done_word                                            736     769     +33
test_main                                            405     418     +13
parse_stream                                        2227    2238     +11
ops_texts                                            124     133      +9
ops_table                                             80      86      +6
run_pipe                                            1557    1562      +5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 8/0 up/down: 269/0)             Total: 269 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-10-31 03:34:07 +01:00
Denys Vlasenko
e16f7eb596 hush: output bash-compat killing signal names
This significantly syncronises ash-signals and hush-signals tests.

function                                             old     new   delta
process_wait_result                                  449     450      +1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-10-24 04:26:43 +02:00
Denys Vlasenko
b65d6cb00f hush: do not print killing signal name in cmd_whihc_dies_on_signal
function                                             old     new   delta
process_wait_result                                  438     449     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-10-24 03:33:32 +02:00
Denys Vlasenko
d4dd48f294 shell: add testsuite for "wait $pid" waiting for other tasks
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-09-29 22:36:36 +02:00
Christian Eggers
39925026f6 shell: Fix "read -d ''" behavior
With bash's read builtin it is possible to read from a file (e.g.
device-tree) until the first '\0' character:

IFS= read -r -d '' VARIABLE < file

In busybox ash the -d extension is also implemented, but checking the
read character for '\0' has to be performed after comparing with the
delimiter.

Signed-off-by: Christian Eggers <ceggers@arri.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-07-31 18:42:30 +02:00
Denys Vlasenko
9aa751b08a shells: fix exitcode_trapN tests to avoid races
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-26 09:05:52 +01:00
Denys Vlasenko
45dd87aac0 ash: expand: Ensure result is escaped in cvtnum
Upstream commit:

    Date: Fri, 1 Jun 2018 18:25:29 +0800
    expand: Ensure result is escaped in cvtnum

    The minus sign generated from arithmetic expansion is currently
    unquoted which causes anomalies when the result is used in where
    the quoting matters.

    This patch fixes it by explicitly calling memtodest on the result
    in cvtnum.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-21 16:30:44 +01:00
Denys Vlasenko
3ced804e31 hush: make "exit" in trap use pre-trap exitcode - fix for nested trap
function                                             old     new   delta
check_and_run_traps                                  276     278      +2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-21 02:55:53 +01:00
Denys Vlasenko
bb095f4838 hush: implement "return NUM in trap sets $? after trap"
function                                             old     new   delta
builtin_return                                        47      67     +20
check_and_run_traps                                  243     259     +16
run_pipe                                            1583    1597     +14
hush_main                                           1076    1086     +10
run_list                                            1054    1055      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/0 up/down: 61/0)               Total: 61 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-20 16:37:59 +01:00
Denys Vlasenko
c91950f315 ash,hush: testcase for "exit" without arguments in a trap
hush fails this one

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-19 12:10:41 +01:00
Denys Vlasenko
9ee5892798 ash: expand: Fix trailing newlines processing in backquote expanding
Upstream commit:

    Date: Mon, 29 Apr 2019 19:13:37 +0500
    expand: Fix trailing newlines processing in backquote expanding

    According to POSIX.1-2008 we should remove newlines only at the end of
    the substitution. Newlines-only substitions causes dash to remove
    newlines before beggining of the substitution. The following code:

        cat <<END
        1
        $(echo "")
        2
        END

    prints "1<newline>2" instead of expected "1<newline><newline>2".

    This patch fixes trailing newlines processing in backquote expanding.

    Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 10:24:32 +01:00
Denys Vlasenko
c2ce888030 ash: parser: Only accept single-digit parameter expansion outside of braces
Upstream commit:

    Date: Mon, 27 May 2019 13:39:37 +0800
    parser: Only accept single-digit parameter expansion outside of braces

    This patch should fix the problem.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 10:15:35 +01:00
Denys Vlasenko
21806562ca hush: restore redirected stdin
function                                             old     new   delta
restore_redirects                                     52      95     +43
save_fd_on_redirect                                  243     253     +10
hfopen                                                90      99      +9
fgetc_interactive                                    259     261      +2
builtin_type                                         117     115      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 64/-2)              Total: 62 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-11-01 14:16:07 +01:00
Denys Vlasenko
30a4c32a4d hush: remove test for "echo ${-}" errorring out - now it works
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-05-19 16:35:56 +02:00
Denys Vlasenko
9e0adb9b09 hush: fix quoted "${notexist-}" expansion to not disappear
function                                             old     new   delta
expand_one_var                                      2296    2311     +15

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-05-15 13:39:19 +02:00
Denys Vlasenko
63d765e666 shells: add tests for backslashes in export VAR=VAL
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-05-14 19:15:20 +02:00
Denys Vlasenko
eb54ca8be0 ash: expand: Do not quote backslashes in unquoted parameter expansion
Upstream commit:

    Date: Wed, 28 Mar 2018 18:37:51 +0800
    expand: Do not quote backslashes in unquoted parameter expansion

    Here is a better example:

        a="/*/\nullx" b="/*/\null"; printf "%s\n" $a $b

    dash currently prints

        /*/\nullx
        /*/\null

    bash prints

        /*/\nullx
        /dev/null

    You may argue the bash behaviour is inconsistent but it actually
    makes sense.  What happens is that quote removal only applies to
    the original token as seen by the shell.  It is never applied to
    the result of parameter expansion.

    Now you may ask why on earth does the second line say "/dev/null"
    instead of "/dev/\null".  Well that's because it is not the quote
    removal step that removed the backslash, but the pathname expansion.

    The fact that the /de\v does not become /dev even though it exists
    is just the result of the optimisation to avoid unnecessarily
        calling stat(2).  I have checked POSIX and I don't see anything
    that forbids this behaviour.

    So going back to dash yes I think we should adopt the bash behaviour
    for pathname expansion and keep the existing case semantics.

    This patch does exactly that.  Note that this patch does not work
    unless you have already applied

        https://patchwork.kernel.org/patch/10306507/

    because otherwise the optimisation mentioned above does not get
    detected correctly and we will end up doing quote removal twice.

    This patch also updates expmeta to handle naked backslashes at
    the end of the pattern which is now possible.

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

function                                             old     new   delta
expmeta                                              618     653     +35
memtodest                                            146     147      +1

Tested to work with both ASH_INTERNAL_GLOB on and off.

hush does not handle this correctly.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-07 18:58:02 +02:00
Denys Vlasenko
77c18491b8 hush: adopt ash's quote_in_varexp1.tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 20:03:04 +02:00
Denys Vlasenko
440da97ed7 ash: expand: Fix ghost fields with unquoted $@/$*
Upstream commit:

    Date: Fri, 23 Mar 2018 18:58:47 +0800
    expand: Fix ghost fields with unquoted $@/$*

    You're right.  The proper fix to this is to ensure that nulonly
    is not set in varvalue for $*.  It should only be set for $@ when
    it's inside double quotes.

    In fact there is another bug while we're playing with $@/$*.
    When IFS is set to a non-whitespace character such as :, $*
    outside quotes won't remove empty fields as it should.

    This patch fixes both problems.

    Reported-by: Martijn Dekker <martijn@inlv.org>
    Suggested-by: Harald van Dijk <harald@gigawatt.nl>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
argstr                                              1111    1113      +2
evalvar                                              571     569      -2
varvalue                                             579     576      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 2/-5)               Total: -3 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 14:29:58 +02:00
Denys Vlasenko
9abf53beb4 ash: eval: Variable assignments on functions are no longer persistent
Upstream commit:

    Date: Wed, 4 Apr 2018 17:54:01 +0800
    eval: Variable assignments on functions are no longer persistent

    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>

function                                             old     new   delta
evalcommand                                         1606    1635     +29
evalcase                                             313     317      +4
evalfun                                              280     268     -12
pushlocalvars                                         48       -     -48
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 2/1 up/down: 33/-60)            Total: -27 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 11:14:11 +02:00
Denys Vlasenko
c2aa218f23 ash,hush: properly handle ${v//pattern/repl} if pattern starts with /
Closes 2695

function                                             old     new   delta
parse_dollar                                         762     790     +28
subevalvar                                          1258    1267      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 37/0)               Total: 37 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-04 22:25:28 +02:00
Denys Vlasenko
a5db1d7354 hush: fix another case where empty "for" wasn't setting exitcode to 0
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-28 12:42:08 +02:00
Denys Vlasenko
63c42afaa4 hush: add "heredoc.tests" from ash, tweak ash "is a function" message
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 17:10:18 +02:00
Denys Vlasenko
41ef41b3e0 hush: fix nested redirects colliding with script fds
This necessitates switch from libc FILE api to a simple
homegrown replacement.
The change which fixes the bug here is the deleting of

	restore_redirected_FILEs();

line. It was prematurely moving (restoring) script fd#3.
The fix is: we don't even _want_ to restore scrit fds,
we are perfectly fine with them being moved.
The only reason we tried to restore them is that FILE api
did not allow moving of FILE->fd.

function                                             old     new   delta
refill_HFILE_and_getc                                  -      93     +93
hfopen                                                 -      90     +90
hfclose                                                -      66     +66
pseudo_exec_argv                                     591     597      +6
hush_main                                           1089    1095      +6
builtin_source                                       209     214      +5
save_fd_on_redirect                                  197     200      +3
setup_redirects                                      320     321      +1
fgetc_interactive                                    235     236      +1
i_peek_and_eat_bkslash_nl                             99      97      -2
expand_vars_to_list                                 1103    1100      -3
restore_redirects                                     99      52     -47
fclose_and_forget                                     57       -     -57
remember_FILE                                         63       -     -63
------------------------------------------------------------------------------
(add/remove: 3/2 grow/shrink: 6/3 up/down: 271/-172)           Total: 99 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 16:54:41 +02:00
Denys Vlasenko
dfc7394763 hush: handle backslash-newline in heredoc terminators
function                                             old     new   delta
fetch_heredocs                                       479     527     +48

(ash fails this test)

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 14:03:18 +02:00
Denys Vlasenko
474cb20555 hush: fix handling of heredocs not enclosed in groups where they are "declared"
function                                             old     new   delta
fetch_heredocs                                         -     479    +479
parse_and_run_stream                                 146     148      +2
parse_stream                                        2787    2296    -491
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 481/-491)          Total: -10 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 13:03:03 +02:00
Denys Vlasenko
3675c37b9b hush: fix heredoc handling in the "cmd <<EOF ;<newline>" case
function                                             old     new   delta
parse_stream                                        2759    2787     +28

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-23 16:31:21 +02:00
Denys Vlasenko
d73cdbf84c hush: fix handling of heredocs starting with empty lines
function                                             old     new   delta
parse_stream                                        2748    2759     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-23 15:43:57 +02:00
Denys Vlasenko
f36caa4071 hush: never glob result of dquoted "${v:+/bin/c*}"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 19:29:41 +02:00
Denys Vlasenko
83e434d5b5 hush: fix handling of '' in ${var:+ARG}
This wasn't an ash bug in dollar_altvalue9, it was hush bug (and bash!)

function                                             old     new   delta
expand_one_var                                      2236    2254     +18
expand_vars_to_list                                 1097    1103      +6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 24/0)               Total: 24 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 17:36:06 +02:00
Denys Vlasenko
294eb4612c hush: fix word splitting in ${v:+ARG} - dollar_altvalue1 test
ash might be a bit buggy, need to investigate dollar_altvalue9 test

function                                             old     new   delta
expand_one_var                                      1639    2236    +597
expand_variables                                     112     128     +16
expand_vars_to_list                                 1117    1097     -20
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 613/-20)           Total: 593 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:29:43 +02:00