]> git.cworth.org Git - tar/blob - gnu/xvasprintf.c
upstream: Fix extraction of device nodes.
[tar] / gnu / xvasprintf.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* vasprintf and asprintf with out-of-memory checking.
4    Copyright (C) 1999, 2002-2004, 2006-2010 Free Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "xvasprintf.h"
23
24 #include <errno.h>
25 #include <limits.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "xalloc.h"
30
31 /* Checked size_t computations.  */
32 #include "xsize.h"
33
34 static inline char *
35 xstrcat (size_t argcount, va_list args)
36 {
37   char *result;
38   va_list ap;
39   size_t totalsize;
40   size_t i;
41   char *p;
42
43   /* Determine the total size.  */
44   totalsize = 0;
45   va_copy (ap, args);
46   for (i = argcount; i > 0; i--)
47     {
48       const char *next = va_arg (ap, const char *);
49       totalsize = xsum (totalsize, strlen (next));
50     }
51   va_end (ap);
52
53   /* Test for overflow in the summing pass above or in (totalsize + 1) below.
54      Also, don't return a string longer than INT_MAX, for consistency with
55      vasprintf().  */
56   if (totalsize == SIZE_MAX || totalsize > INT_MAX)
57     {
58       errno = EOVERFLOW;
59       return NULL;
60     }
61
62   /* Allocate and fill the result string.  */
63   result = XNMALLOC (totalsize + 1, char);
64   p = result;
65   for (i = argcount; i > 0; i--)
66     {
67       const char *next = va_arg (args, const char *);
68       size_t len = strlen (next);
69       memcpy (p, next, len);
70       p += len;
71     }
72   *p = '\0';
73
74   return result;
75 }
76
77 char *
78 xvasprintf (const char *format, va_list args)
79 {
80   char *result;
81
82   /* Recognize the special case format = "%s...%s".  It is a frequently used
83      idiom for string concatenation and needs to be fast.  We don't want to
84      have a separate function xstrcat() for this purpose.  */
85   {
86     size_t argcount = 0;
87     const char *f;
88
89     for (f = format;;)
90       {
91         if (*f == '\0')
92           /* Recognized the special case of string concatenation.  */
93           return xstrcat (argcount, args);
94         if (*f != '%')
95           break;
96         f++;
97         if (*f != 's')
98           break;
99         f++;
100         argcount++;
101       }
102   }
103
104   if (vasprintf (&result, format, args) < 0)
105     {
106       if (errno == ENOMEM)
107         xalloc_die ();
108       return NULL;
109     }
110
111   return result;
112 }