From f5200a50fa42fdaed40ecc67b27f41be328a6a01 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 7 Oct 2004 16:15:22 +0000 Subject: Avoid a memory allocation when possible. --- lib/snprintf.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib/snprintf.c') diff --git a/lib/snprintf.c b/lib/snprintf.c index 667ed94625..9a4edc1f5f 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -42,19 +42,19 @@ snprintf (char *str, size_t size, const char *format, ...) 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; } -- cgit v1.2.1