Debian Codegraph @

lib/xvasprintf.c

raw
1 /* vasprintf and asprintf with out-of-memory checking.
2 Copyright (C) 1999, 2002-2004, 2006-2026 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 3 of the License, or
7 (at your option) 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
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification. */
20 #include "xvasprintf.h"
21
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "xalloc.h"
29
30 /* Checked size_t computations. */
31 #include "xsize.h"
32
33 static char *
34 xstrcat (size_t argcount, va_list args)
35 {
36 /* Determine the total size. */
37 size_t totalsize = 0;
38 {
39 va_list ap;
40 va_copy (ap, args);
41 for (size_t i = argcount; i > 0; i--)
42 {
43 const char *next = va_arg (ap, const char *);
44 totalsize = xsum (totalsize, strlen (next));
45 }
46 va_end (ap);
47 }
48
49 /* Test for overflow in the summing pass above or in (totalsize + 1)
50 below. */
51 if (totalsize == SIZE_MAX)
52 xalloc_die ();
53
54 /* Allocate and fill the result string. */
55 char *result = XNMALLOC (totalsize + 1, char);
56 {
57 char *p = result;
58 for (size_t i = argcount; i > 0; i--)
59 {
60 const char *next = va_arg (args, const char *);
61 size_t len = strlen (next);
62 memcpy (p, next, len);
63 p += len;
64 }
65 *p = '\0';
66 }
67
68 return result;
69 }
70
71 char *
72 xvasprintf (const char *format, va_list args)
73 {
74
75 /* Recognize the special case format = "%s...%s". It is a frequently used
76 idiom for string concatenation and needs to be fast. We don't want to
77 have a separate function xstrcat() for this purpose. */
78 {
79 size_t argcount = 0;
80
81 for (const char *f = format;;)
82 {
83 if (*f == '\0')
84 /* Recognized the special case of string concatenation. */
85 return xstrcat (argcount, args);
86 if (*f != '%')
87 break;
88 f++;
89 if (*f != 's')
90 break;
91 f++;
92 argcount++;
93 }
94 }
95
96 char *result;
97 if (vaszprintf (&result, format, args) < 0)
98 {
99 if (errno == ENOMEM)
100 xalloc_die ();
101 else
102 {
103 /* The programmer ought to have ensured that none of the other errors
104 can occur. */
105 int err = errno;
106 char errbuf[20];
107 const char *errname;
108 #if HAVE_WORKING_STRERRORNAME_NP
109 errname = strerrorname_np (err);
110 if (errname == NULL)
111 #else
112 if (err == EINVAL)
113 errname = "EINVAL";
114 else if (err == EILSEQ)
115 errname = "EILSEQ";
116 else if (err == EOVERFLOW)
117 errname = "EOVERFLOW";
118 else
119 #endif
120 {
121 sprintf (errbuf, "%d", err);
122 errname = errbuf;
123 }
124 fprintf (stderr, "vasprintf failed! format=\"%s\", errno=%s\n",
125 format, errname);
126 fflush (stderr);
127 abort ();
128 }
129 }
130
131 return result;
132 }