From aec76b2b6e79e36dafced60fd91a877f5db98c4e Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Wed, 13 Aug 2014 06:06:02 +0300 Subject: Minor improvement in gsub() memory management. --- builtin.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index 077575fa..0995fdf7 100644 --- a/builtin.c +++ b/builtin.c @@ -2786,16 +2786,11 @@ set_how_many: text = t->stptr; textlen = t->stlen; - buflen = textlen + 2; repl = s->stptr; replend = repl + s->stlen; repllen = replend - repl; - emalloc(buf, char *, buflen + 2, "do_sub"); - buf[buflen] = '\0'; - buf[buflen + 1] = '\0'; - ampersands = 0; /* @@ -2854,6 +2849,13 @@ set_how_many: } lastmatchnonzero = false; + + /* guesstimate how much room to allocate; +2 forces > 0 */ + buflen = textlen + (ampersands + 1) * repllen + 2; + emalloc(buf, char *, buflen + 2, "do_sub"); + buf[buflen] = '\0'; + buf[buflen + 1] = '\0'; + bp = buf; for (current = 1;; current++) { matches++; -- cgit v1.2.3