aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2024-07-13 00:59:02 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2024-07-13 00:59:02 +0200
commit14e28c18ca1a0fb87b6c73d5cb7487e80bc6713a (patch)
tree5dbbdf0d23281b1409b558e26043c4895b44a6a6
parent6c38d0e9da2d4a3defd2bff9f3e00f6229e9824b (diff)
downloadbusybox-w32-14e28c18ca1a0fb87b6c73d5cb7487e80bc6713a.tar.gz
busybox-w32-14e28c18ca1a0fb87b6c73d5cb7487e80bc6713a.tar.bz2
busybox-w32-14e28c18ca1a0fb87b6c73d5cb7487e80bc6713a.zip
hush: fix "exec 3>FILE" aborting if 3 is exactly the next free fd
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--shell/ash_test/ash-redir/redir3.right2
-rw-r--r--shell/ash_test/ash-redir/redir_exec2.right4
-rwxr-xr-xshell/ash_test/ash-redir/redir_exec2.tests11
-rw-r--r--shell/ash_test/ash-redir/redir_to_bad_fd255.right2
-rw-r--r--shell/ash_test/ash-redir/redir_to_bad_fd3.right2
-rw-r--r--shell/hush.c13
-rw-r--r--shell/hush_test/hush-redir/redir_exec2.right4
-rwxr-xr-xshell/hush_test/hush-redir/redir_exec2.tests11
8 files changed, 42 insertions, 7 deletions
diff --git a/shell/ash_test/ash-redir/redir3.right b/shell/ash_test/ash-redir/redir3.right
index fd641a8ea..d9cf5dac5 100644
--- a/shell/ash_test/ash-redir/redir3.right
+++ b/shell/ash_test/ash-redir/redir3.right
@@ -1,3 +1,3 @@
1TEST 1TEST
2./redir3.tests: line 4: 9: Bad file descriptor 2./redir3.tests: line 4: dup2(9,1): Bad file descriptor
3Output to fd#9: 1 3Output to fd#9: 1
diff --git a/shell/ash_test/ash-redir/redir_exec2.right b/shell/ash_test/ash-redir/redir_exec2.right
new file mode 100644
index 000000000..2bdc093c9
--- /dev/null
+++ b/shell/ash_test/ash-redir/redir_exec2.right
@@ -0,0 +1,4 @@
1fd/5
2fd/4
3fd/3
4One:1
diff --git a/shell/ash_test/ash-redir/redir_exec2.tests b/shell/ash_test/ash-redir/redir_exec2.tests
new file mode 100755
index 000000000..700a51786
--- /dev/null
+++ b/shell/ash_test/ash-redir/redir_exec2.tests
@@ -0,0 +1,11 @@
1cd /proc/$$
2exec 5>/dev/null
3exec 4>/dev/null
4exec 3>/dev/null
5ls -1 fd/5
6ls -1 fd/4
7ls -1 fd/3
8exec 5>&-
9test -e fd/5 && echo BUG
10echo One:$?
11
diff --git a/shell/ash_test/ash-redir/redir_to_bad_fd255.right b/shell/ash_test/ash-redir/redir_to_bad_fd255.right
index 9c5e35b36..ca4df91ac 100644
--- a/shell/ash_test/ash-redir/redir_to_bad_fd255.right
+++ b/shell/ash_test/ash-redir/redir_to_bad_fd255.right
@@ -1,2 +1,2 @@
1./redir_to_bad_fd255.tests: line 2: 255: Bad file descriptor 1./redir_to_bad_fd255.tests: line 2: dup2(255,1): Bad file descriptor
2OK 2OK
diff --git a/shell/ash_test/ash-redir/redir_to_bad_fd3.right b/shell/ash_test/ash-redir/redir_to_bad_fd3.right
index 895a4a0a6..5120322a5 100644
--- a/shell/ash_test/ash-redir/redir_to_bad_fd3.right
+++ b/shell/ash_test/ash-redir/redir_to_bad_fd3.right
@@ -1,2 +1,2 @@
1./redir_to_bad_fd3.tests: line 2: 3: Bad file descriptor 1./redir_to_bad_fd3.tests: line 2: dup2(3,1): Bad file descriptor
2OK 2OK
diff --git a/shell/hush.c b/shell/hush.c
index afbc3ebec..1d5642260 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -8077,8 +8077,11 @@ static int internally_opened_fd(int fd, struct squirrel *sq)
8077 return 0; 8077 return 0;
8078} 8078}
8079 8079
8080/* squirrel != NULL means we squirrel away copies of stdin, stdout, 8080/* sqp != NULL means we squirrel away copies of stdin, stdout,
8081 * and stderr if they are redirected. */ 8081 * and stderr if they are redirected.
8082 * If redirection fails, return 1. This will make caller
8083 * skip command execution and restore already created redirect fds.
8084 */
8082static int setup_redirects(struct command *prog, struct squirrel **sqp) 8085static int setup_redirects(struct command *prog, struct squirrel **sqp)
8083{ 8086{
8084 struct redir_struct *redir; 8087 struct redir_struct *redir;
@@ -8109,7 +8112,7 @@ static int setup_redirects(struct command *prog, struct squirrel **sqp)
8109 * "cmd > <file" (2nd redirect starts too early) 8112 * "cmd > <file" (2nd redirect starts too early)
8110 */ 8113 */
8111 syntax_error("invalid redirect"); 8114 syntax_error("invalid redirect");
8112 continue; 8115 return 1;
8113 } 8116 }
8114 mode = redir_table[redir->rd_type].mode; 8117 mode = redir_table[redir->rd_type].mode;
8115 p = expand_string_to_string(redir->rd_filename, 8118 p = expand_string_to_string(redir->rd_filename,
@@ -8124,7 +8127,9 @@ static int setup_redirects(struct command *prog, struct squirrel **sqp)
8124 */ 8127 */
8125 return 1; 8128 return 1;
8126 } 8129 }
8127 if (newfd == redir->rd_fd && sqp) { 8130 if (newfd == redir->rd_fd && sqp
8131 && sqp != ERR_PTR /* not a redirect in "exec" */
8132 ) {
8128 /* open() gave us precisely the fd we wanted. 8133 /* open() gave us precisely the fd we wanted.
8129 * This means that this fd was not busy 8134 * This means that this fd was not busy
8130 * (not opened to anywhere). 8135 * (not opened to anywhere).
diff --git a/shell/hush_test/hush-redir/redir_exec2.right b/shell/hush_test/hush-redir/redir_exec2.right
new file mode 100644
index 000000000..2bdc093c9
--- /dev/null
+++ b/shell/hush_test/hush-redir/redir_exec2.right
@@ -0,0 +1,4 @@
1fd/5
2fd/4
3fd/3
4One:1
diff --git a/shell/hush_test/hush-redir/redir_exec2.tests b/shell/hush_test/hush-redir/redir_exec2.tests
new file mode 100755
index 000000000..700a51786
--- /dev/null
+++ b/shell/hush_test/hush-redir/redir_exec2.tests
@@ -0,0 +1,11 @@
1cd /proc/$$
2exec 5>/dev/null
3exec 4>/dev/null
4exec 3>/dev/null
5ls -1 fd/5
6ls -1 fd/4
7ls -1 fd/3
8exec 5>&-
9test -e fd/5 && echo BUG
10echo One:$?
11