From: Bruno Haible Date: Tue, 9 May 2006 17:29:04 +0000 (+0000) Subject: Tweak xstrcat function. X-Git-Tag: v0.15~183 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0705d8bd9ba0988ef428d31355496de0b75fb11;p=thirdparty%2Fgettext.git Tweak xstrcat function. --- diff --git a/gettext-tools/lib/ChangeLog b/gettext-tools/lib/ChangeLog index 02f32ea7a..7278d3233 100644 --- a/gettext-tools/lib/ChangeLog +++ b/gettext-tools/lib/ChangeLog @@ -1,3 +1,9 @@ +2006-05-09 Bruno Haible + + * xvasprintf.c (xstrcat): Handle overflow. Suggested by Paul Eggert. + Optimize away a va_copy call. Suggested by Eric Blake. Add missing + va_end call. + 2006-05-06 Charles Wilson * progreloc.c (maybe_executable) [CYGWIN]: Use the access() function. diff --git a/gettext-tools/lib/xvasprintf.c b/gettext-tools/lib/xvasprintf.c index 4e95375dc..d68a50d83 100644 --- a/gettext-tools/lib/xvasprintf.c +++ b/gettext-tools/lib/xvasprintf.c @@ -54,10 +54,12 @@ xstrcat (size_t argcount, va_list args) const char *next = va_arg (ap, const char *); totalsize = xsum (totalsize, strlen (next)); } + va_end (ap); - /* Don't return a string longer than INT_MAX, for consistency with + /* Test for overflow in the summing pass above or in (totalsize + 1) below. + Also, don't return a string longer than INT_MAX, for consistency with vasprintf(). */ - if (totalsize > INT_MAX) + if (totalsize == SIZE_MAX || totalsize > INT_MAX) { errno = EOVERFLOW; return NULL; @@ -66,10 +68,9 @@ xstrcat (size_t argcount, va_list args) /* Allocate and fill the result string. */ result = (char *) xmalloc (totalsize + 1); p = result; - va_copy (ap, args); for (i = argcount; i > 0; i--) { - const char *next = va_arg (ap, const char *); + const char *next = va_arg (args, const char *); size_t len = strlen (next); memcpy (p, next, len); p += len;