From 1dd6cf867754030223fadd0f4d959039f713bfb1 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 2 May 2009 14:17:31 +0200 Subject: hush: fix multiple redirections of the same fd (bug 227) Signed-off-by: Denys Vlasenko --- shell/hush.c | 8 ++++++-- shell/hush_test/hush-misc/redir6.right | 4 ++++ shell/hush_test/hush-misc/redir6.tests | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 shell/hush_test/hush-misc/redir6.right create mode 100755 shell/hush_test/hush-misc/redir6.tests (limited to 'shell') diff --git a/shell/hush.c b/shell/hush.c index cbec2dd71..5daca960c 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -2646,7 +2646,9 @@ static int setup_redirects(struct command *prog, int squirrel[]) for (redir = prog->redirects; redir; redir = redir->next) { if (redir->rd_type == REDIRECT_HEREDOC2) { /* rd_fd<rd_fd < 3) { + if (squirrel && redir->rd_fd < 3 + && squirrel[redir->rd_fd] < 0 + ) { squirrel[redir->rd_fd] = dup(redir->rd_fd); } /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ @@ -2682,7 +2684,9 @@ static int setup_redirects(struct command *prog, int squirrel[]) } if (openfd != redir->rd_fd) { - if (squirrel && redir->rd_fd < 3) { + if (squirrel && redir->rd_fd < 3 + && squirrel[redir->rd_fd] < 0 + ) { squirrel[redir->rd_fd] = dup(redir->rd_fd); } if (openfd == REDIRFD_CLOSE) { diff --git a/shell/hush_test/hush-misc/redir6.right b/shell/hush_test/hush-misc/redir6.right new file mode 100644 index 000000000..a97c4bdf1 --- /dev/null +++ b/shell/hush_test/hush-misc/redir6.right @@ -0,0 +1,4 @@ +Testing multiple redirections to same fd +Hello +Done1 +Done2 diff --git a/shell/hush_test/hush-misc/redir6.tests b/shell/hush_test/hush-misc/redir6.tests new file mode 100755 index 000000000..c639ebb2d --- /dev/null +++ b/shell/hush_test/hush-misc/redir6.tests @@ -0,0 +1,5 @@ +echo "Testing multiple redirections to same fd" +# bug was making us lose fd #1 after this: +echo Hello >/dev/null 1>&2 +echo Done1 +echo Done2 >&2 -- cgit v1.2.3