aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-01-09 08:13:21 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2017-01-09 08:13:21 +0100
commit2b1559056cf32c42675ecd937796e1455bcb5c2c (patch)
tree6dc78577d23802be73dbb03990e5352c1d4fa323 /shell
parent4e4f88e569e6e32669c856a86c60bb3fc104d588 (diff)
downloadbusybox-2b1559056cf32c42675ecd937796e1455bcb5c2c.tar.gz
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>
Diffstat (limited to 'shell')
-rw-r--r--shell/ash_test/ash-misc/source_argv_and_shift.right4
-rwxr-xr-xshell/ash_test/ash-misc/source_argv_and_shift.tests12
-rw-r--r--shell/hush.c6
-rw-r--r--shell/hush_test/hush-misc/source_argv_and_shift.right4
-rwxr-xr-xshell/hush_test/hush-misc/source_argv_and_shift.tests12
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 @@
+sourced_arg1:1
+arg1:
+sourced_arg1:a
+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 @@
+echo 'echo sourced_arg1:$1' >sourced1
+echo 'shift' >>sourced1
+
+set -- 1
+. ./sourced1
+echo arg1:$1
+
+set -- 1
+. ./sourced1 a
+echo arg1:$1
+
+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)
char *arg_path, *filename;
FILE *input;
save_arg_t sv;
+ char *args_need_save;
#if ENABLE_HUSH_FUNCTIONS
smallint sv_flg;
#endif
@@ -9637,7 +9638,8 @@ static int FAST_FUNC builtin_source(char **argv)
/* "we are inside sourced file, ok to use return" */
G_flag_return_in_progress = -1;
#endif
- if (argv[1])
+ args_need_save = argv[1]; /* used as a boolean variable */
+ if (args_need_save)
save_and_replace_G_args(&sv, argv);
/* "false; . ./empty_line; echo Zero:$?" should print 0 */
@@ -9645,7 +9647,7 @@ static int FAST_FUNC builtin_source(char **argv)
parse_and_run_file(input);
fclose_and_forget(input);
- if (argv[1])
+ if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */
restore_G_args(&sv, argv);
#if ENABLE_HUSH_FUNCTIONS
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 @@
+sourced_arg1:1
+arg1:
+sourced_arg1:a
+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 @@
+echo 'echo sourced_arg1:$1' >sourced1
+echo 'shift' >>sourced1
+
+set -- 1
+. ./sourced1
+echo arg1:$1
+
+set -- 1
+. ./sourced1 a
+echo arg1:$1
+
+rm sourced1