Source-Changes-HG archive

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

[src/trunk]: src/bin/sh PR/45269: Andreas Gustafsson: Instead of falling off ...



details:   https://anonhg.NetBSD.org/src/rev/94727ba9a746
branches:  trunk
changeset: 768623:94727ba9a746
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Aug 23 10:04:39 2011 +0000

description:
PR/45269: Andreas Gustafsson: Instead of falling off the edge when eating trailing newlines
if the block has moved, arrange so that trailing newlines are never placed in the string
in the first place, by accumulating them and adding them only after we've encountered a
non-newline character. This allows also for more efficient appending since we know how much
we need beforehand. From FreeBSD.

diffstat:

 bin/sh/expand.c |  26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diffs (64 lines):

diff -r c669890b3fe9 -r 94727ba9a746 bin/sh/expand.c
--- a/bin/sh/expand.c   Tue Aug 23 10:01:32 2011 +0000
+++ b/bin/sh/expand.c   Tue Aug 23 10:04:39 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expand.c,v 1.84 2011/06/18 21:18:46 christos Exp $     */
+/*     $NetBSD: expand.c,v 1.85 2011/08/23 10:04:39 christos 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.84 2011/06/18 21:18:46 christos Exp $");
+__RCSID("$NetBSD: expand.c,v 1.85 2011/08/23 10:04:39 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -426,6 +426,7 @@
        char const *syntax = quoted? DQSYNTAX : BASESYNTAX;
        int saveherefd;
        int quotes = flag & (EXP_FULL | EXP_CASE);
+       int nnl;
 
        INTOFF;
        saveifs = ifsfirst;
@@ -443,6 +444,7 @@
 
        p = in.buf;
        lastc = '\0';
+       nnl = 0;
        for (;;) {
                if (--in.nleft < 0) {
                        if (in.fd < 0)
@@ -456,17 +458,21 @@
                }
                lastc = *p++;
                if (lastc != '\0') {
-                       if (quotes && syntax[(int)lastc] == CCTL)
-                               STPUTC(CTLESC, dest);
-                       STPUTC(lastc, dest);
+                       if (lastc == '\n')
+                               nnl++;
+                       else {
+                               CHECKSTRSPACE(nnl + 2, dest);
+                               while (nnl > 0) {
+                                       nnl--;
+                                       USTPUTC('\n', dest);
+                               }
+                               if (quotes && syntax[(int)lastc] == CCTL)
+                                       USTPUTC(CTLESC, dest);
+                               USTPUTC(lastc, dest);
+                       }
                }
        }
 
-       /* Eat all trailing newlines */
-       p = stackblock() + startloc;
-       while (dest > p && dest[-1] == '\n')
-               STUNPUTC(dest);
-
        if (in.fd >= 0)
                close(in.fd);
        if (in.buf)



Home | Main Index | Thread Index | Old Index