From patchwork Fri Jan 17 09:57:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 11338673 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E81496C1 for ; Fri, 17 Jan 2020 09:57:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D04982073A for ; Fri, 17 Jan 2020 09:57:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726587AbgAQJ56 (ORCPT ); Fri, 17 Jan 2020 04:57:58 -0500 Received: from helcar.hmeau.com ([216.24.177.18]:51698 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726566AbgAQJ56 (ORCPT ); Fri, 17 Jan 2020 04:57:58 -0500 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtps (Exim 4.89 #2 (Debian)) id 1isOO4-0000xX-NR; Fri, 17 Jan 2020 17:57:56 +0800 Received: from herbert by gondobar with local (Exim 4.89) (envelope-from ) id 1isOO3-000568-En; Fri, 17 Jan 2020 17:57:55 +0800 Date: Fri, 17 Jan 2020 17:57:55 +0800 From: Herbert Xu To: Harald van Dijk Cc: DASH shell mailing list Subject: [PATCH] redir: Clear saved redirections in subshell Message-ID: <20200117095755.y6mwflmqqw7zeegh@gondor.apana.org.au> References: <20200117085124.ub53qala7c636owf@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200117085124.ub53qala7c636owf@gondor.apana.org.au> User-Agent: NeoMutt/20170113 (1.7.2) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org On Fri, Jan 17, 2020 at 04:51:24PM +0800, Herbert Xu wrote: > On Mon, Jan 06, 2020 at 09:57:20PM +0000, Harald van Dijk wrote: > > > > One way to fix this is to install an exception handler in evaltree(nr) to > > prevent exceptions bubbling up too far. It can just call exitshell() from > > there. It is not clear to me yet whether this is the best way. > > Thanks for the report. I think what we should do is drop the > relevant state when we enter the subshell. Something like this should fix the redir problem. I'll address the other issue in a subsequent patch. ---8<--- When we enter a subshell we need to drop the saved redirections as otherwise a subsequent unwindredir could produce incorrect results. This patch does this by simply clearing redirlist. While we could actually free the memory underneath for subshells it isn't really worth the trouble for now. Reported-by: Harald van Dijk Signed-off-by: Herbert Xu diff --git a/src/jobs.c b/src/jobs.c index 26a6248..69a511c 100644 --- a/src/jobs.c +++ b/src/jobs.c @@ -859,6 +859,7 @@ static void forkchild(struct job *jp, union node *n, int mode) closescript(); clear_traps(); + clearredir(); #if JOBS /* do job control only in root shell */ diff --git a/src/redir.c b/src/redir.c index 6c81dd0..8242b9c 100644 --- a/src/redir.c +++ b/src/redir.c @@ -66,15 +66,7 @@ # define PIPESIZE PIPE_BUF #endif - -MKINIT -struct redirtab { - struct redirtab *next; - int renamed[10]; -}; - - -MKINIT struct redirtab *redirlist; +struct redirtab *redirlist; /* Bit map of currently closed file descriptors. */ static unsigned closed_redirs; diff --git a/src/redir.h b/src/redir.h index 8e56995..82c8ea8 100644 --- a/src/redir.h +++ b/src/redir.h @@ -41,13 +41,22 @@ #endif #define REDIR_SAVEFD2 03 /* set preverrout */ -struct redirtab; +struct redirtab { + struct redirtab *next; + int renamed[10]; +}; + +extern struct redirtab *redirlist; + union node; void redirect(union node *, int); void popredir(int); -void clearredir(void); int savefd(int, int); int redirectsafe(union node *, int); void unwindredir(struct redirtab *stop); struct redirtab *pushredir(union node *redir); +static inline void clearredir(void) +{ + redirlist = NULL; +}