From patchwork Wed Dec 14 01:06:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?0L3QsNCx?= X-Patchwork-Id: 13072667 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F108C4332F for ; Wed, 14 Dec 2022 01:06:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237605AbiLNBGJ (ORCPT ); Tue, 13 Dec 2022 20:06:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237331AbiLNBGH (ORCPT ); Tue, 13 Dec 2022 20:06:07 -0500 Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0ECDC1FCFE for ; Tue, 13 Dec 2022 17:06:07 -0800 (PST) Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id 5D0CE1420; Wed, 14 Dec 2022 02:06:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nabijaczleweli.xyz; s=202211; t=1670979966; bh=WYphJDsAiwrtHaq2L27c0S7jMvUZ+oLXrAjgTK9Gv8o=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=XxEHUzNc5oNyru68snMZV/IZXrM5msWBcDbIAlbBMUbqQxHaCsRFzthZik/aX582u azpRJiDc8u2A+n9LvcstvT2jGpAxsVZ5TQcQ1rmaCIjbJk/au0ankjbXUCd5iJPTpu PTJMyWuGK1/uZVVAJpa3V3VOMoz4t+ND1XftBzm8ENM9swx8BKg/qVUM1HZk39tIfc QWcdnuB9OGnlkZLhkR5dgLcxwGEEAol4KMETYeR4AAE6WHngepnvxzlxBlCnh7dCBW bX0UtVHTcewPr4gnYdFK0ZIsHhIQz6C7luHxFe+zuPNsrrnRWeVcoDpAf7nJYSJ6lF nVgN8PAQT8hGg== Date: Wed, 14 Dec 2022 02:06:05 +0100 From: =?utf-8?b?0L3QsNCx?= To: dash@vger.kernel.org Cc: Harald van Dijk Subject: [PATCH v2 1/3] parser: fixredir: invalid redirections are run-time, not syntax Message-ID: <3a7ba3fa786e34cde8ab158cb8578f0d39cf655e.1670979949.git.nabijaczleweli@nabijaczleweli.xyz> References: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz> User-Agent: NeoMutt/20220429 Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org This fixes a long-standing bug where echo 'echo >&a' | sh errors out with sh: 2: Syntax error: Bad fd number despite the error being on line 1 This patch makes the error sh: 1: Bad fd number: a as expected Adapted-from: https://github.com/hvdijk/gwsh/commit/d279523041c1c380d64b6dec7760feba20bbf6b5 --- src/parser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.c b/src/parser.c index a552c47..8a06b9e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -615,7 +615,7 @@ void fixredir(union node *n, const char *text, int err) else { if (err) - synerror("Bad fd number"); + sh_error("Bad fd number: %s", text); else n->ndup.vname = makename(); } From patchwork Wed Dec 14 01:06:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?0L3QsNCx?= X-Patchwork-Id: 13072668 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4DFCC4167B for ; Wed, 14 Dec 2022 01:06:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237331AbiLNBGO (ORCPT ); Tue, 13 Dec 2022 20:06:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237622AbiLNBGN (ORCPT ); Tue, 13 Dec 2022 20:06:13 -0500 Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C98E720F74 for ; Tue, 13 Dec 2022 17:06:11 -0800 (PST) Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id 2512D1422; Wed, 14 Dec 2022 02:06:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nabijaczleweli.xyz; s=202211; t=1670979971; bh=q9xUjh5EnLv9HcTDjy1D6ieekRuLfieCXp/n5xPLsvk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=SCPN+d3k8RXnj3JTL6hzvRrR+CElAtrPFSNsoNtODh5kz04M67ZeRH8cc3lUuNcjN mzVme3s7c0tDWgEpvrS2g4y5MyRCeixtSNZHsDjEeHVKIMRzbDzZSg35wsW/JS9uJR UtK7S/PpfPBhW1TvyU02yxds0KG5zv8cJu9H864XBPcAl04yOGeydjT/nGgyVr9EOR y03zE5j+eI+ccqOlQKlHSuKUr1S6bWCXb9O8CmsfHX18mFJ5TqzUIgzYaU4QwdXZgZ +eXORUUZIam7aIp6VhH51XEFpHDUh3JO6I7M0Bave8CSvm74cQtzv5rZw9CKLRLECh Gbn679zyS5ljQ== Date: Wed, 14 Dec 2022 02:06:10 +0100 From: =?utf-8?b?0L3QsNCx?= To: dash@vger.kernel.org Cc: Harald van Dijk Subject: [PATCH v2 2/3] parser: synerror: explicitly consume the entire invalid line Message-ID: References: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz> User-Agent: NeoMutt/20220429 Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org Interactively, sh_error() doesn't terminate, so echo "|$(printf %10000s)echo bug" | sh -i would read the first 8KiB, see that it's invalid, then jump back to the parser, which would then read and execute the rest of the line as-if it were the next line. The fix for this is to explicitly consume the rest of the invalid line, so that the next line observed is /actually/ the next line. This is difficult to trigger accidentally right now, since we consume the entire icanon line buffer at once (provided it's <8k, which it ~always is interactively), so we always observe one line at a time, but the next patch would make even "| echo bug" blow up. Imported-from: https://github.com/hvdijk/gwsh/commit/d279523041c1c380d64b6dec7760feba20bbf6b5 --- src/parser.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/parser.c b/src/parser.c index 8a06b9e..35fdbc3 100644 --- a/src/parser.c +++ b/src/parser.c @@ -761,6 +761,13 @@ static void nlnoprompt(void) needprompt = doprompt; } +static void +skipline(void) +{ + int c; + while ((c = pgetc()) != '\n' && c != PEOF); +} + /* * Read the next input token. @@ -798,7 +805,7 @@ xxreadtoken(void) case ' ': case '\t': continue; case '#': - while ((c = pgetc()) != '\n' && c != PEOF); + skipline(); pungetc(); continue; case '\n': @@ -1526,6 +1533,12 @@ STATIC void synerror(const char *msg) { errlinno = plinno; + + /* If we see a syntax error in a command, read the rest of the + * line now before reporting the error. This ensures we get error + * reporting that does not depend on buffering details. */ + skipline(); + sh_error("Syntax error: %s", msg); /* NOTREACHED */ } From patchwork Wed Dec 14 01:06:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?0L3QsNCx?= X-Patchwork-Id: 13072669 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0E23C4332F for ; Wed, 14 Dec 2022 01:06:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237644AbiLNBG1 (ORCPT ); Tue, 13 Dec 2022 20:06:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237624AbiLNBGR (ORCPT ); Tue, 13 Dec 2022 20:06:17 -0500 Received: from tarta.nabijaczleweli.xyz (unknown [139.28.40.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id ADE451D302 for ; Tue, 13 Dec 2022 17:06:16 -0800 (PST) Received: from tarta.nabijaczleweli.xyz (unknown [192.168.1.250]) by tarta.nabijaczleweli.xyz (Postfix) with ESMTPSA id 047361424; Wed, 14 Dec 2022 02:06:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nabijaczleweli.xyz; s=202211; t=1670979976; bh=Ym0a7d4SCo8fZNa76cLln2JmAYoCNhoWl1FcrOiHFH8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=TxGpSJtOYDmGq12Y9UEbhBX1I/C0cm/kiZZMkBZEVK3LYq5hoot0MifosiqEaa9kJ QUuLaxhgUTxUDEGNwTWpDuqDMM5y6KOKclwTq9jdWQEvjCShbDvQ+xEJjvEHUw5hSE gkvRcnOhq8s/RkHIPfy41Mn6BKJlL7QFGXGnrbD9mhWS959h2ErJ7HgmipI7xERrhy /T8ZUDIn/XgVGYcepv8TBocHaBA9Uexj83quhbHFMh5SPZDT+uGjsnWfrbwaYiNvlD Lyh4JeRfiTmgks/g0MVNRm06WhL6xMBdQWdBXvqpyI5sH1918p6sDRCPXdC+T4I19b xmjmpAt6N5mXg== Date: Wed, 14 Dec 2022 02:06:14 +0100 From: =?utf-8?b?0L3QsNCx?= To: dash@vger.kernel.org Cc: Harald van Dijk Subject: [PATCH v2 3/3] input: preadfd: read standard input byte-wise Message-ID: <42d2b629549d8be07d14acb6cdd7d0f853ed1a6e.1670979949.git.nabijaczleweli@nabijaczleweli.xyz> References: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz> User-Agent: NeoMutt/20220429 Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org POSIX Issue 7, XCU, sh, STDIN says: When the shell is using standard input and it invokes a command that also uses standard input, the shell shall ensure that the standard input file pointer points directly after the command it has read when the command begins execution. It shall not read ahead in such a manner that any characters intended to be read by the invoked command are consumed by the shell (whether interpreted by the shell or not) or that characters that are not read by the invoked command are not seen by the shell. I.e. sh <fd, buf, IBUFSIZ - 1); + nr = read(parsefile->fd, buf, parsefile->fd == 0 ? 1 : IBUFSIZ - 1); if (nr < 0) {