From 5b762f1a64d25a6e2e06009ede925a9c5057dc57 Mon Sep 17 00:00:00 2001 From: Simon Schubert Date: Mon, 26 Oct 2009 11:28:35 +0100 Subject: [PATCH] sh: fix spurious newline slips in backquote expansion From FreeBSD revision 1.43: Instead of eating trailing newlines after inserting them into the output buffer, don't insert them at all. This prevents a buffer *underrun* when the substitution consists completely of newlines (e.g. `echo`) and the byte before the source buffer to which p points is a '\n', in which case more characters would be removed from the output buffer than were inserted. DragonFly-bug: --- bin/sh/expand.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bin/sh/expand.c b/bin/sh/expand.c index 1163314ce6..4d3c56b23f 100644 --- a/bin/sh/expand.c +++ b/bin/sh/expand.c @@ -428,6 +428,7 @@ expbackq(union node *cmd, int quoted, int flag) char const *syntax = quoted? DQSYNTAX : BASESYNTAX; int saveherefd; int quotes = flag & (EXP_FULL | EXP_CASE | EXP_REDIR); + int nnl; INTOFF; saveifs = ifsfirst; @@ -445,6 +446,8 @@ expbackq(union node *cmd, int quoted, int flag) p = in.buf; lastc = '\0'; + nnl = 0; + /* Don't copy trailing newlines */ for (;;) { if (--in.nleft < 0) { if (in.fd < 0) @@ -460,14 +463,18 @@ expbackq(union node *cmd, int quoted, int flag) if (lastc != '\0') { if (quotes && syntax[(int)lastc] == CCTL) STPUTC(CTLESC, dest); - STPUTC(lastc, dest); + if (lastc == '\n') { + nnl++; + } else { + while (nnl > 0) { + nnl--; + STPUTC('\n', dest); + } + STPUTC(lastc, dest); + } } } - /* Eat all trailing newlines */ - for ( ; (dest - stackblock()) > startloc && *(dest-1) == '\n'; ) - STUNPUTC(dest); - if (in.fd >= 0) close(in.fd); if (in.buf) -- 2.41.0