annotate lib/xvasprintf.c @ 40231:9b3c79fdfe0b

strtod: fix clash with strtold Problem reported for RHEL 5 by Jesse Caldwell (Bug#34817). * lib/strtod.c (compute_minus_zero, minus_zero): Simplify by remving the macro / external variable, and having just a function. User changed. This avoids the need for an external variable that might clash.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 11 Mar 2019 16:40:29 -0700
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* vasprintf and asprintf with out-of-memory checking.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 1999, 2002-2004, 2006-2019 Free Software Foundation, Inc.
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8570
diff changeset
4 This program is free software: you can redistribute it and/or modify
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8570
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8570
diff changeset
7 (at your option) any later version.
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8570
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
7510
6c0cb059c9ea * lib/xvasprintf.c (includes): Assume config.h.
Eric Blake <ebb9@byu.net>
parents: 6779
diff changeset
17 #include <config.h>
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include "xvasprintf.h"
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <errno.h>
6779
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
23 #include <limits.h>
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
24 #include <string.h>
8570
4175c39ba7cc Move vasprintf prototypes to stdio.
Simon Josefsson <simon@josefsson.org>
parents: 7603
diff changeset
25 #include <stdio.h>
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
5272
533332af517b Fixing a stupid typo in xvasprintf.c... from Oskar Liljeblad.
Bruno Haible <bruno@clisp.org>
parents: 5218
diff changeset
27 #include "xalloc.h"
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
6779
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
29 /* Checked size_t computations. */
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
30 #include "xsize.h"
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
31
17191
d27d22e71282 xvasprintf: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
32 static char *
6779
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
33 xstrcat (size_t argcount, va_list args)
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
34 {
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
35 char *result;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
36 va_list ap;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
37 size_t totalsize;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
38 size_t i;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
39 char *p;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
40
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
41 /* Determine the total size. */
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
42 totalsize = 0;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
43 va_copy (ap, args);
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
44 for (i = argcount; i > 0; i--)
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
45 {
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
46 const char *next = va_arg (ap, const char *);
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
47 totalsize = xsum (totalsize, strlen (next));
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
48 }
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
49 va_end (ap);
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
50
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
51 /* Test for overflow in the summing pass above or in (totalsize + 1) below.
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
52 Also, don't return a string longer than INT_MAX, for consistency with
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
53 vasprintf(). */
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
54 if (totalsize == SIZE_MAX || totalsize > INT_MAX)
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
55 {
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
56 errno = EOVERFLOW;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
57 return NULL;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
58 }
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
59
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
60 /* Allocate and fill the result string. */
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7510
diff changeset
61 result = XNMALLOC (totalsize + 1, char);
6779
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
62 p = result;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
63 for (i = argcount; i > 0; i--)
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
64 {
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
65 const char *next = va_arg (args, const char *);
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
66 size_t len = strlen (next);
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
67 memcpy (p, next, len);
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
68 p += len;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
69 }
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
70 *p = '\0';
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
71
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
72 return result;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
73 }
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
74
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 char *
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 xvasprintf (const char *format, va_list args)
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 {
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 char *result;
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79
6779
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
80 /* Recognize the special case format = "%s...%s". It is a frequently used
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
81 idiom for string concatenation and needs to be fast. We don't want to
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
82 have a separate function xstrcat() for this purpose. */
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
83 {
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
84 size_t argcount = 0;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
85 const char *f;
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
86
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
87 for (f = format;;)
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
88 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
89 if (*f == '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
90 /* Recognized the special case of string concatenation. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
91 return xstrcat (argcount, args);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
92 if (*f != '%')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
93 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
94 f++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
95 if (*f != 's')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
96 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
97 f++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
98 argcount++;
6779
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
99 }
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
100 }
1807d27bf1ec Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
101
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 if (vasprintf (&result, format, args) < 0)
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 {
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 if (errno == ENOMEM)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9831
diff changeset
105 xalloc_die ();
5218
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 return NULL;
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 return result;
f96c64693f86 New module 'xvasprintf'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 }