diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2016-05-03 12:31:52 -0400 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2016-05-03 12:31:52 -0400 |
commit | a6df7afc605079df7d85318846a522ef64aaa44d (patch) | |
tree | 15ed9f57bccf32dd1a65066eb6b6e97f658337fa | |
parent | ffb353c999bd3d5785b92a3aad78a96ff03a4ced (diff) | |
download | egawk-a6df7afc605079df7d85318846a522ef64aaa44d.tar.gz egawk-a6df7afc605079df7d85318846a522ef64aaa44d.tar.bz2 egawk-a6df7afc605079df7d85318846a522ef64aaa44d.zip |
Fix sprintf memory leak by using realloc to shrink buffer to the proper size.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | builtin.c | 7 |
2 files changed, 12 insertions, 2 deletions
@@ -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 @@ -660,7 +660,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; @@ -1572,7 +1572,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: { |