1 /* vasprintf and asprintf with out-of-memory checking.
2 Copyright (C) 1999, 2002-2004, 2006, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "xvasprintf.h"
30 /* Checked size_t computations. */
33 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
35 # define EOVERFLOW E2BIG
39 xstrcat (size_t argcount, va_list args)
47 /* Determine the total size. */
50 for (i = argcount; i > 0; i--)
52 const char *next = va_arg (ap, const char *);
53 totalsize = xsum (totalsize, strlen (next));
57 /* Test for overflow in the summing pass above or in (totalsize + 1) below.
58 Also, don't return a string longer than INT_MAX, for consistency with
60 if (totalsize == SIZE_MAX || totalsize > INT_MAX)
66 /* Allocate and fill the result string. */
67 result = XNMALLOC (totalsize + 1, char);
69 for (i = argcount; i > 0; i--)
71 const char *next = va_arg (args, const char *);
72 size_t len = strlen (next);
73 memcpy (p, next, len);
82 xvasprintf (const char *format, va_list args)
86 /* Recognize the special case format = "%s...%s". It is a frequently used
87 idiom for string concatenation and needs to be fast. We don't want to
88 have a separate function xstrcat() for this purpose. */
96 /* Recognized the special case of string concatenation. */
97 return xstrcat (argcount, args);
108 if (vasprintf (&result, format, args) < 0)