Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh PR bin/55979



details:   https://anonhg.NetBSD.org/src/rev/6c7db7a76d25
branches:  trunk
changeset: 1018775:6c7db7a76d25
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Feb 16 15:30:26 2021 +0000

description:
PR bin/55979

Correctly handle (ie: ignore completely) \0 chars (nuls) in the
shell command input stream (script, dot file, or stdin).

Previously nul chars were ignored correctly in the line in which
they occurred, but would cause trailing chars of that line to reappear
as the start of the following line.   If there was just one \0 skipped,
this would generally result in an extra \n in the sh input, which in
most cases has no effect.   With multiple \0's in a single line, more
of the end of that line was duplicated into the following one.  This
usually manifested as a weird "command not found" error.

Note that any \0 chars in the sh input make the script non-conforming,
so fixing this is not crucial (no \0's should really ever be seen) but
it was an obvious bug in the code, which was attempting to ignore nul
chars (as do many other shells), so let it be fixed.

XXX pullup -9

diffstat:

 bin/sh/input.c |  17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)

diffs (61 lines):

diff -r 2dc310693ef4 -r 6c7db7a76d25 bin/sh/input.c
--- a/bin/sh/input.c    Tue Feb 16 15:30:12 2021 +0000
+++ b/bin/sh/input.c    Tue Feb 16 15:30:26 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: input.c,v 1.71 2019/02/09 09:20:47 kre Exp $   */
+/*     $NetBSD: input.c,v 1.72 2021/02/16 15:30:26 kre Exp $   */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)input.c    8.3 (Berkeley) 6/9/95";
 #else
-__RCSID("$NetBSD: input.c,v 1.71 2019/02/09 09:20:47 kre Exp $");
+__RCSID("$NetBSD: input.c,v 1.72 2021/02/16 15:30:26 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -90,6 +90,7 @@
        int fd;                 /* file descriptor (or -1 if string) */
        int nleft;              /* number of chars left in this line */
        int lleft;              /* number of chars left in this buffer */
+       int nskip;              /* number of \0's dropped in previous line */
        const char *nextc;      /* next char in buffer */
        char *buf;              /* input buffer */
        struct strpush *strpush; /* for pushing strings at this level */
@@ -274,8 +275,13 @@
                        parselleft = parsenleft = EOF_NLEFT;
                        return PEOF;
                }
+               parsefile->nskip = 0;
        }
 
+               /* jump over slots for any \0 chars that were dropped */
+       parsenextc += parsefile->nskip;
+       parsefile->nskip = 0;
+
                /* p = (not const char *)parsenextc; */
        p = parsefile->buf + (parsenextc - parsefile->buf);
        q = p;
@@ -288,6 +294,7 @@
                switch (*p) {
                case '\0':
                        p++;    /* Skip nul */
+                       parsefile->nskip++;
                        goto check;
 
                case '\t':
@@ -306,7 +313,11 @@
                        break;
                }
 
-               *q++ = *p++;
+               if (parsefile->nskip)
+                       *q++ = *p++;
+               else
+                       q = ++p;
+
  check:
                if (--parselleft <= 0) {
                        parsenleft = q - parsenextc - 1;



Home | Main Index | Thread Index | Old Index