diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-09 08:13:21 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-09 08:13:21 +0100 |
commit | 2b1559056cf32c42675ecd937796e1455bcb5c2c (patch) | |
tree | 6dc78577d23802be73dbb03990e5352c1d4fa323 | |
parent | 4e4f88e569e6e32669c856a86c60bb3fc104d588 (diff) | |
download | busybox-w32-2b1559056cf32c42675ecd937796e1455bcb5c2c.tar.gz busybox-w32-2b1559056cf32c42675ecd937796e1455bcb5c2c.tar.bz2 busybox-w32-2b1559056cf32c42675ecd937796e1455bcb5c2c.zip |
hush: fix a bug in argv restoration after sourcing a file
if sourced file "shift"ed argvs so that $1 is NULL, restore wasn't done.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash_test/ash-misc/source_argv_and_shift.right | 4 | ||||
-rwxr-xr-x | shell/ash_test/ash-misc/source_argv_and_shift.tests | 12 | ||||
-rw-r--r-- | shell/hush.c | 6 | ||||
-rw-r--r-- | shell/hush_test/hush-misc/source_argv_and_shift.right | 4 | ||||
-rwxr-xr-x | shell/hush_test/hush-misc/source_argv_and_shift.tests | 12 |
5 files changed, 36 insertions, 2 deletions
diff --git a/shell/ash_test/ash-misc/source_argv_and_shift.right b/shell/ash_test/ash-misc/source_argv_and_shift.right new file mode 100644 index 000000000..b15cc96e7 --- /dev/null +++ b/shell/ash_test/ash-misc/source_argv_and_shift.right | |||
@@ -0,0 +1,4 @@ | |||
1 | sourced_arg1:1 | ||
2 | arg1: | ||
3 | sourced_arg1:a | ||
4 | arg1:1 | ||
diff --git a/shell/ash_test/ash-misc/source_argv_and_shift.tests b/shell/ash_test/ash-misc/source_argv_and_shift.tests new file mode 100755 index 000000000..66353f3d7 --- /dev/null +++ b/shell/ash_test/ash-misc/source_argv_and_shift.tests | |||
@@ -0,0 +1,12 @@ | |||
1 | echo 'echo sourced_arg1:$1' >sourced1 | ||
2 | echo 'shift' >>sourced1 | ||
3 | |||
4 | set -- 1 | ||
5 | . ./sourced1 | ||
6 | echo arg1:$1 | ||
7 | |||
8 | set -- 1 | ||
9 | . ./sourced1 a | ||
10 | echo arg1:$1 | ||
11 | |||
12 | rm sourced1 | ||
diff --git a/shell/hush.c b/shell/hush.c index c69e4ec8a..5c5715b3f 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -9606,6 +9606,7 @@ static int FAST_FUNC builtin_source(char **argv) | |||
9606 | char *arg_path, *filename; | 9606 | char *arg_path, *filename; |
9607 | FILE *input; | 9607 | FILE *input; |
9608 | save_arg_t sv; | 9608 | save_arg_t sv; |
9609 | char *args_need_save; | ||
9609 | #if ENABLE_HUSH_FUNCTIONS | 9610 | #if ENABLE_HUSH_FUNCTIONS |
9610 | smallint sv_flg; | 9611 | smallint sv_flg; |
9611 | #endif | 9612 | #endif |
@@ -9637,7 +9638,8 @@ static int FAST_FUNC builtin_source(char **argv) | |||
9637 | /* "we are inside sourced file, ok to use return" */ | 9638 | /* "we are inside sourced file, ok to use return" */ |
9638 | G_flag_return_in_progress = -1; | 9639 | G_flag_return_in_progress = -1; |
9639 | #endif | 9640 | #endif |
9640 | if (argv[1]) | 9641 | args_need_save = argv[1]; /* used as a boolean variable */ |
9642 | if (args_need_save) | ||
9641 | save_and_replace_G_args(&sv, argv); | 9643 | save_and_replace_G_args(&sv, argv); |
9642 | 9644 | ||
9643 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ | 9645 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
@@ -9645,7 +9647,7 @@ static int FAST_FUNC builtin_source(char **argv) | |||
9645 | parse_and_run_file(input); | 9647 | parse_and_run_file(input); |
9646 | fclose_and_forget(input); | 9648 | fclose_and_forget(input); |
9647 | 9649 | ||
9648 | if (argv[1]) | 9650 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
9649 | restore_G_args(&sv, argv); | 9651 | restore_G_args(&sv, argv); |
9650 | #if ENABLE_HUSH_FUNCTIONS | 9652 | #if ENABLE_HUSH_FUNCTIONS |
9651 | G_flag_return_in_progress = sv_flg; | 9653 | G_flag_return_in_progress = sv_flg; |
diff --git a/shell/hush_test/hush-misc/source_argv_and_shift.right b/shell/hush_test/hush-misc/source_argv_and_shift.right new file mode 100644 index 000000000..b15cc96e7 --- /dev/null +++ b/shell/hush_test/hush-misc/source_argv_and_shift.right | |||
@@ -0,0 +1,4 @@ | |||
1 | sourced_arg1:1 | ||
2 | arg1: | ||
3 | sourced_arg1:a | ||
4 | arg1:1 | ||
diff --git a/shell/hush_test/hush-misc/source_argv_and_shift.tests b/shell/hush_test/hush-misc/source_argv_and_shift.tests new file mode 100755 index 000000000..66353f3d7 --- /dev/null +++ b/shell/hush_test/hush-misc/source_argv_and_shift.tests | |||
@@ -0,0 +1,12 @@ | |||
1 | echo 'echo sourced_arg1:$1' >sourced1 | ||
2 | echo 'shift' >>sourced1 | ||
3 | |||
4 | set -- 1 | ||
5 | . ./sourced1 | ||
6 | echo arg1:$1 | ||
7 | |||
8 | set -- 1 | ||
9 | . ./sourced1 a | ||
10 | echo arg1:$1 | ||
11 | |||
12 | rm sourced1 | ||