From de29cd0869e37df15a49283781c378181cdd0c93 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Thu, 18 May 2017 21:44:14 +0000 Subject: [PATCH] sh: Ensure memout.bufsize matches allocated buffer, if it exists. --- bin/sh/eval.c | 3 +-- bin/sh/output.c | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/sh/eval.c b/bin/sh/eval.c index f637cfab5fd9..9386b16f0b82 100644 --- a/bin/sh/eval.c +++ b/bin/sh/eval.c @@ -1081,8 +1081,6 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) mode = (cmdentry.u.index == EXECCMD)? 0 : REDIR_PUSH; if (flags == EV_BACKCMD) { memout.nextc = memout.buf; - memout.bufend = memout.buf; - memout.bufsize = 64; mode |= REDIR_BACKQ; } savecmdname = commandname; @@ -1139,6 +1137,7 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) memout.buf = NULL; memout.nextc = NULL; memout.bufend = NULL; + memout.bufsize = 64; } if (cmdentry.u.index != EXECCMD) popredir(); diff --git a/bin/sh/output.c b/bin/sh/output.c index 64d311f0c2d6..c3458272785e 100644 --- a/bin/sh/output.c +++ b/bin/sh/output.c @@ -73,7 +73,7 @@ static int doformat_wr(void *, const char *, int); struct output output = {NULL, NULL, NULL, OUTBUFSIZ, 1, 0}; struct output errout = {NULL, NULL, NULL, 256, 2, 0}; -struct output memout = {NULL, NULL, NULL, 0, MEM_OUT, 0}; +struct output memout = {NULL, NULL, NULL, 64, MEM_OUT, 0}; struct output *out1 = &output; struct output *out2 = &errout; @@ -208,7 +208,7 @@ outbin(const void *data, size_t len, struct output *file) void emptyoutbuf(struct output *dest) { - int offset; + int offset, newsize; if (dest->buf == NULL) { INTOFF; @@ -218,10 +218,11 @@ emptyoutbuf(struct output *dest) INTON; } else if (dest->fd == MEM_OUT) { offset = dest->nextc - dest->buf; + newsize = dest->bufsize << 1; INTOFF; - dest->bufsize <<= 1; - dest->buf = ckrealloc(dest->buf, dest->bufsize); - dest->bufend = dest->buf + dest->bufsize; + dest->buf = ckrealloc(dest->buf, newsize); + dest->bufsize = newsize; + dest->bufend = dest->buf + newsize; dest->nextc = dest->buf + offset; INTON; } else {