Mercurial > hg > octave-nkf > gnulib-hg
changeset 5333:f038f3333cc8
Avoid a memory allocation when possible.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 07 Oct 2004 16:15:22 +0000 |
parents | e28d7c1dc1d3 |
children | c6b509e118ef |
files | lib/ChangeLog lib/snprintf.c |
diffstat | 2 files changed, 11 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2004-10-07 Bruno Haible <bruno@clisp.org> + + * snprintf.c (snprintf): Avoid a memory allocation if the result fits + into the provided buffer. + 2004-10-06 Paul Eggert <eggert@cs.ucla.edu> * diacrit.c, diacrit.h: Add GPL notice.
--- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -42,19 +42,19 @@ va_list args; va_start (args, format); - output = vasnprintf (NULL, &len, format, args); + len = size; + output = vasnprintf (str, &len, format, args); va_end (args); if (!output) return -1; - if (str && size > 0) - { - memcpy (str, output, MIN (len + 1, size)); + if (str != NULL) + if (len > size - 1) /* equivalent to: (size > 0 && len >= size) */ str[size - 1] = '\0'; - } - free (output); + if (output != str) + free (output); return len; }