Source-Changes-HG archive

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

[src/trunk]: src/bin/sh When expanding a here-doc (NXHERE - the type with an ...



details:   https://anonhg.NetBSD.org/src/rev/c0645341e72d
branches:  trunk
changeset: 744806:c0645341e72d
user:      kre <kre%NetBSD.org@localhost>
date:      Thu Feb 13 05:19:05 2020 +0000

description:
When expanding a here-doc (NXHERE - the type with an unquoted end delim)
the output will not be further processed (at all) so there is no need
to escape magic chars in the output, and doing so leaves stray CTLESC
chars in the here doc text.  Not good.   So don't do that...

To save a strlen() of the result, to determine the size of the here doc,
make rmescapes() return the length of the resulting string (this isn't
needed for other uses, so didn't happen previously).

Reported on current-users@ (2020-02-06) by Jun Ebihara

XXX pullup -9

diffstat:

 bin/sh/expand.c |  18 ++++++++++++------
 bin/sh/expand.h |   4 ++--
 2 files changed, 14 insertions(+), 8 deletions(-)

diffs (88 lines):

diff -r 3fe52fe6c3c3 -r c0645341e72d bin/sh/expand.c
--- a/bin/sh/expand.c   Thu Feb 13 02:53:46 2020 +0000
+++ b/bin/sh/expand.c   Thu Feb 13 05:19:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expand.c,v 1.136 2019/10/14 13:34:14 christos Exp $    */
+/*     $NetBSD: expand.c,v 1.137 2020/02/13 05:19:05 kre Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)expand.c   8.5 (Berkeley) 5/15/95";
 #else
-__RCSID("$NetBSD: expand.c,v 1.136 2019/10/14 13:34:14 christos Exp $");
+__RCSID("$NetBSD: expand.c,v 1.137 2020/02/13 05:19:05 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -146,10 +146,12 @@
 void
 expandhere(union node *arg, int fd)
 {
+       int len;
 
        herefd = fd;
        expandarg(arg, NULL, 0);
-       xwrite(fd, stackblock(), expdest - stackblock());
+       len = rmescapes(stackblock());
+       xwrite(fd, stackblock(),  len);
 }
 
 
@@ -307,7 +309,7 @@
                        had_dol_at = 0;
                        break;
                case CTLESC:
-                       if (quotes || ISCTL(*p))
+                       if ((quotes || ISCTL(*p)))
                                STPUTC(c, expdest);
                        c = *p++;
                        STPUTC(c, expdest);
@@ -2037,9 +2039,11 @@
 
 /*
  * Remove any CTLESC or CTLNONL characters from a string.
+ *
+ * String is modified in place, and we return the length of the result
  */
 
-void
+int
 rmescapes(char *str)
 {
        char *p, *q;
@@ -2047,7 +2051,7 @@
        p = str;
        while (!ISCTL(*p)) {
                if (*p++ == '\0')
-                       return;
+                       return ((int)(p - str) - 1);
        }
        q = p;
        while (*p) {
@@ -2069,6 +2073,8 @@
                *q++ = *p++;
        }
        *q = '\0';
+
+       return ((int)(q - str));
 }
 
 /*
diff -r 3fe52fe6c3c3 -r c0645341e72d bin/sh/expand.h
--- a/bin/sh/expand.h   Thu Feb 13 02:53:46 2020 +0000
+++ b/bin/sh/expand.h   Thu Feb 13 05:19:05 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expand.h,v 1.24 2018/11/18 17:23:37 kre Exp $  */
+/*     $NetBSD: expand.h,v 1.25 2020/02/13 05:19:05 kre Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -67,5 +67,5 @@
 
 void expandhere(union node *, int);
 void expandarg(union node *, struct arglist *, int);
-void rmescapes(char *);
+int rmescapes(char *);
 int casematch(union node *, char *);



Home | Main Index | Thread Index | Old Index