From patchwork Fri Dec 14 01:01:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martijn Dekker X-Patchwork-Id: 10730185 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ECCB26C5 for ; Fri, 14 Dec 2018 01:01:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D84D72CF0D for ; Fri, 14 Dec 2018 01:01:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9C472CF2C; Fri, 14 Dec 2018 01:01:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 68D322CF0D for ; Fri, 14 Dec 2018 01:01:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728388AbeLNBBU (ORCPT ); Thu, 13 Dec 2018 20:01:20 -0500 Received: from kahlil.inlv.org ([37.59.109.123]:46512 "EHLO kahlil.inlv.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727638AbeLNBBU (ORCPT ); Thu, 13 Dec 2018 20:01:20 -0500 Received: from [192.168.1.250] (host109-153-139-242.range109-153.btcentralplus.com [109.153.139.242]) (authenticated bits=0) by kahlil.inlv.org (8.15.2/8.15.2) with ESMTPSA id wBE11DO6022188 (version=TLSv1.2 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Fri, 14 Dec 2018 02:01:13 +0100 Subject: [PATCH v2] Create block-local FD state when appending redirection closing the FD To: Herbert Xu , Harald van Dijk Cc: dash@vger.kernel.org References: <20180919042550.6z2rhszi4m5r6vqa@gondor.apana.org.au> From: Martijn Dekker Message-ID: <7bd61c96-8c14-aaba-d410-1ec4335a0608@inlv.org> Date: Fri, 14 Dec 2018 01:01:12 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20180919042550.6z2rhszi4m5r6vqa@gondor.apana.org.au> Content-Language: en-GB Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP [Corrected patch due to an oops in first take. Apologies.] Op 19-09-18 om 05:25 schreef Herbert Xu: > Harald van Dijk wrote: >> On 23/04/2018 19:56, Martijn Dekker wrote: >>> $ dash -c '{ exec 8>> Output: "oops, still open" >>> Expected output: Bad file descriptor >>> >>> Apparently, dash either fails to push the file descriptor onto the stack >>> at '} 8<&-', or fails to restore it. >>> >>> Same bug with loops ending in "done 8<&-". >>> >>> Confirmed in all dash versions down to 0.5.5.1. >> >> What surprises me most is that dash has code written specifically to >> keep the fd closed. dash would be smaller and simpler if it behaved the >> way you expected and the way most other shells behave: just remove all >> traces of REALLY_CLOSED. Probably a lingering case of bug-compatibility with the original Bourne shell, which behaves this way (confirmed on a VM with 1988 Xenix). > So did anything happen on the bash front? I'm happy to change if > bash moves in the same direction. Yes. According to the bash changelog, Chet fixed it in git last 30th of April, meaning it'll be in bash 5.0. Patch attached, as per Harald's suggestion. - Martijn diff --git a/src/redir.c b/src/redir.c index e67cc0a..1e3feac 100644 --- a/src/redir.c +++ b/src/redir.c @@ -57,7 +57,6 @@ #include "error.h" -#define REALLY_CLOSED -3 /* fd that was closed and still is */ #define EMPTY -2 /* marks an unused slot in redirtab */ #define CLOSED -1 /* fd opened for redir needs to be closed */ @@ -136,10 +135,6 @@ redirect(union node *redir, int flags) } } - if (i == newfd) - /* Can only happen if i == newfd == CLOSED */ - i = REALLY_CLOSED; - *p = i; } @@ -352,7 +347,6 @@ popredir(int drop) close(i); break; case EMPTY: - case REALLY_CLOSED: break; default: if (!drop)