Module Name:    src
Committed By:   christos
Date:           Tue Aug 23 10:04:39 UTC 2011

Modified Files:
        src/bin/sh: expand.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.84 -r1.85 src/bin/sh/expand.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/expand.c
diff -u src/bin/sh/expand.c:1.84 src/bin/sh/expand.c:1.85
--- src/bin/sh/expand.c:1.84	Sat Jun 18 17:18:46 2011
+++ src/bin/sh/expand.c	Tue Aug 23 06:04:39 2011
@@ -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)

Reply via email to