goo/GooString.cc | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/goo/GooString.cc b/goo/GooString.cc index 8591d95..106857a 100644 --- a/goo/GooString.cc +++ b/goo/GooString.cc @@ -320,10 +320,11 @@ GooString *GooString::appendfv(const char *fmt, va_list argList) { int len, i; const char *p0, *p1; char *str; + GooStringFormatArg argsBuf[ 8 ]; argsLen = 0; - argsSize = 8; - args = (GooStringFormatArg *)gmallocn(argsSize, sizeof(GooStringFormatArg)); + argsSize = sizeof(argsBuf) / sizeof(argsBuf[0]); + args = argsBuf; p0 = fmt; while (*p0) { @@ -392,8 +393,13 @@ GooString *GooString::appendfv(const char *fmt, va_list argList) { if (idx == argsLen) { if (argsLen == argsSize) { argsSize *= 2; - args = (GooStringFormatArg *)greallocn(args, argsSize, + if (args == argsBuf) { + args = (GooStringFormatArg *)gmallocn(argsSize, sizeof(GooStringFormatArg)); + memcpy(args, argsBuf, argsLen); + } else { + args = (GooStringFormatArg *)greallocn(args, argsSize, sizeof(GooStringFormatArg)); + } } switch (ft) { case fmtIntDecimal: @@ -632,7 +638,10 @@ GooString *GooString::appendfv(const char *fmt, va_list argList) { } } - gfree(args); + if (args != argsBuf) { + gfree(args); + } + return this; }