d68a50d83c61dc79cfe6db4c2248265d0bba598a
[gnulib.git] / lib / xvasprintf.c
1 /* vasprintf and asprintf with out-of-memory checking.
2    Copyright (C) 1999, 2002-2004, 2006 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 /* Specification.  */
23 #include "xvasprintf.h"
24
25 #include <errno.h>
26 #include <limits.h>
27 #include <string.h>
28
29 #include "vasprintf.h"
30 #include "xalloc.h"
31
32 /* Checked size_t computations.  */
33 #include "xsize.h"
34
35 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
36 #ifndef EOVERFLOW
37 # define EOVERFLOW E2BIG
38 #endif
39
40 static inline char *
41 xstrcat (size_t argcount, va_list args)
42 {
43   char *result;
44   va_list ap;
45   size_t totalsize;
46   size_t i;
47   char *p;
48
49   /* Determine the total size.  */
50   totalsize = 0;
51   va_copy (ap, args);
52   for (i = argcount; i > 0; i--)
53     {
54       const char *next = va_arg (ap, const char *);
55       totalsize = xsum (totalsize, strlen (next));
56     }
57   va_end (ap);
58
59   /* Test for overflow in the summing pass above or in (totalsize + 1) below.
60      Also, don't return a string longer than INT_MAX, for consistency with
61      vasprintf().  */
62   if (totalsize == SIZE_MAX || totalsize > INT_MAX)
63     {
64       errno = EOVERFLOW;
65       return NULL;
66     }
67
68   /* Allocate and fill the result string.  */
69   result = (char *) xmalloc (totalsize + 1);
70   p = result;
71   for (i = argcount; i > 0; i--)
72     {
73       const char *next = va_arg (args, const char *);
74       size_t len = strlen (next);
75       memcpy (p, next, len);
76       p += len;
77     }
78   *p = '\0';
79
80   return result;
81 }
82
83 char *
84 xvasprintf (const char *format, va_list args)
85 {
86   char *result;
87
88   /* Recognize the special case format = "%s...%s".  It is a frequently used
89      idiom for string concatenation and needs to be fast.  We don't want to
90      have a separate function xstrcat() for this purpose.  */
91   {
92     size_t argcount = 0;
93     const char *f;
94
95     for (f = format;;)
96       {
97         if (*f == '\0')
98           /* Recognized the special case of string concatenation.  */
99           return xstrcat (argcount, args);
100         if (*f != '%')
101           break;
102         f++;
103         if (*f != 's')
104           break;
105         f++;
106         argcount++;
107       }
108   }
109
110   if (vasprintf (&result, format, args) < 0)
111     {
112       if (errno == ENOMEM)
113         xalloc_die ();
114       return NULL;
115     }
116
117   return result;
118 }