aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--builtin.c7
2 files changed, 12 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 5212622c..bb9cadf6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2016-05-03 Andrew J. Schorr <aschorr@telemetry-investments.com>
+
+ * builtin.c (format_tree): After the string has been rendered, use
+ realloc to shrink the buffer to the needed size. Otherwise, the minimum
+ buffer size of 512 bytes can result in lots of wasted memory if many
+ sprintf results are stored in an array.
+
2016-05-02 Andrew J. Schorr <aschorr@telemetry-investments.com>
* gawkapi.h (gawk_api_major_version, gawk_api_minor_version): Add
diff --git a/builtin.c b/builtin.c
index 4256ee5e..31c13a81 100644
--- a/builtin.c
+++ b/builtin.c
@@ -670,7 +670,7 @@ format_tree(
int i, nc;
bool toofew = false;
char *obuf, *obufout;
- size_t osiz, ofre;
+ size_t osiz, ofre, olen_final;
const char *chbuf;
const char *s0, *s1;
int cs1;
@@ -1582,7 +1582,10 @@ mpf1:
_("too many arguments supplied for format string"));
}
bchunk(s0, s1 - s0);
- r = make_str_node(obuf, obufout - obuf, ALREADY_MALLOCED);
+ olen_final = obufout - obuf;
+ if (ofre > 0)
+ erealloc(obuf, char *, olen_final + 2, "format_tree");
+ r = make_str_node(obuf, olen_final, ALREADY_MALLOCED);
obuf = NULL;
out:
{