All of lore.kernel.org
 help / color / mirror / Atom feed
From: наб <nabijaczleweli@nabijaczleweli.xyz>
To: dash@vger.kernel.org
Cc: Harald van Dijk <harald@gigawatt.nl>
Subject: [PATCH v2 2/3] parser: synerror: explicitly consume the entire invalid line
Date: Wed, 14 Dec 2022 02:06:10 +0100	[thread overview]
Message-ID: <b62afca3e10b03c802276ace6e514f7ccb7a5134.1670979949.git.nabijaczleweli@nabijaczleweli.xyz> (raw)
In-Reply-To: <20221214010539.g3zy2jtzdrhsyrrz@tarta.nabijaczleweli.xyz>

[-- Attachment #1: Type: text/plain, Size: 1781 bytes --]

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 */
 }
-- 
2.30.2


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2022-12-14  1:06 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-13 22:17 [PATCH] input: preadfd: read standard input byte-wise наб
2022-12-13 22:37 ` Harald van Dijk
2022-12-14  1:05   ` наб
2022-12-14  1:06     ` [PATCH v2 1/3] parser: fixredir: invalid redirections are run-time, not syntax наб
2023-01-05  9:43       ` Herbert Xu
2022-12-14  1:06     ` наб [this message]
2023-01-03  1:53       ` [PATCH v2 2/3] parser: synerror: explicitly consume the entire invalid line Herbert Xu
2023-01-03  5:32         ` [PATCH] input: Eat rest of line upon reset Herbert Xu
2023-01-03 11:47         ` [PATCH v2 2/3] parser: synerror: explicitly consume the entire invalid line Harald van Dijk
2023-01-04  9:51           ` Herbert Xu
2023-01-04 11:25             ` Harald van Dijk
2023-01-04 14:10               ` Herbert Xu
2023-01-04 14:30                 ` Harald van Dijk
2023-01-04 14:41                   ` Herbert Xu
2023-01-04 14:59                     ` Harald van Dijk
2023-01-05  7:12                       ` [PATCH] input: Check for int_pending while clearing input Herbert Xu
2022-12-14  1:06     ` [PATCH v2 3/3] input: preadfd: read standard input byte-wise наб
2023-01-03  6:15       ` [v3 PATCH] input: Read " Herbert Xu
2023-01-03 11:54         ` Harald van Dijk
2023-01-04  8:59           ` Herbert Xu
2023-01-04 11:18             ` Harald van Dijk
2023-01-05  7:26               ` [PATCH] input: Only skip blank lines on PS1 Herbert Xu
2023-01-05  8:33                 ` Harald van Dijk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b62afca3e10b03c802276ace6e514f7ccb7a5134.1670979949.git.nabijaczleweli@nabijaczleweli.xyz \
    --to=nabijaczleweli@nabijaczleweli.xyz \
    --cc=dash@vger.kernel.org \
    --cc=harald@gigawatt.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.