annotate lib/c-strtod.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 b716418da8b9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* Convert string to double, using the C locale.
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
2
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
3 Copyright (C) 2003-2004, 2006, 2009-2019 Free Software Foundation, Inc.
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
5 This program is free software: you can redistribute it and/or modify
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 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: 7302
diff changeset
7 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: 7302
diff changeset
8 (at your option) any later version.
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 GNU General Public License for more details.
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 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
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18 /* Written by Paul Eggert. */
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
20 #include <config.h>
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 #include "c-strtod.h"
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
11070
42d47e0b9b24 Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
24 #include <errno.h>
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25 #include <locale.h>
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
26 #include <stdlib.h>
11075
ab7203b2967c Change c_strtod, c_strtold to no longer call xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 11074
diff changeset
27 #include <string.h>
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
28
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29 #if LONG
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30 # define C_STRTOD c_strtold
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
31 # define DOUBLE long double
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
32 # define STRTOD_L strtold_l
40173
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
33 # define HAVE_GOOD_STRTOD_L (HAVE_STRTOLD_L && !GNULIB_defined_strtold_function)
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
34 # define STRTOD strtold
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
35 #else
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
36 # define C_STRTOD c_strtod
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37 # define DOUBLE double
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
38 # define STRTOD_L strtod_l
40173
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
39 # define HAVE_GOOD_STRTOD_L (HAVE_STRTOD_L && !GNULIB_defined_strtod_function)
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
40 # define STRTOD strtod
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41 #endif
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42
40173
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
43 #if defined LC_ALL_MASK \
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
44 && ((LONG ? HAVE_GOOD_STRTOLD_L : HAVE_GOOD_STRTOD_L) \
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
45 || HAVE_WORKING_USELOCALE)
11072
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
46
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
47 /* Cache for the C locale object.
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
48 Marked volatile so that different threads see the same value
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
49 (avoids locking). */
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
50 static volatile locale_t c_locale_cache;
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
51
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
52 /* Return the C locale object, or (locale_t) 0 with errno set
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
53 if it cannot be created. */
17184
dcca7ac14066 c-strtod, memcoll, readutmp: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
54 static locale_t
11072
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
55 c_locale (void)
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
56 {
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
57 if (!c_locale_cache)
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
58 c_locale_cache = newlocale (LC_ALL_MASK, "C", (locale_t) 0);
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
59 return c_locale_cache;
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
60 }
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
61
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
62 #endif
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
63
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
64 DOUBLE
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 C_STRTOD (char const *nptr, char **endptr)
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
66 {
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
67 DOUBLE r;
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
68
40173
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
69 #if defined LC_ALL_MASK \
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
70 && ((LONG ? HAVE_GOOD_STRTOLD_L : HAVE_GOOD_STRTOD_L) \
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
71 || HAVE_WORKING_USELOCALE)
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
72
11072
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
73 locale_t locale = c_locale ();
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
74 if (!locale)
11074
8cf858130820 c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents: 11072
diff changeset
75 {
8cf858130820 c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents: 11072
diff changeset
76 if (endptr)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11082
diff changeset
77 *endptr = (char *) nptr;
11074
8cf858130820 c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents: 11072
diff changeset
78 return 0; /* errno is set here */
8cf858130820 c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents: 11072
diff changeset
79 }
11070
42d47e0b9b24 Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
80
40173
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
81 # if (LONG ? HAVE_GOOD_STRTOLD_L : HAVE_GOOD_STRTOD_L)
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
82
11072
2017a2abcff4 Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents: 11070
diff changeset
83 r = STRTOD_L (nptr, endptr, locale);
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
84
40173
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
85 # else /* HAVE_WORKING_USELOCALE */
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
86
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
87 locale_t old_locale = uselocale (locale);
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
88 if (old_locale == (locale_t)0)
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
89 {
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
90 if (endptr)
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
91 *endptr = (char *) nptr;
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
92 return 0; /* errno is set here */
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
93 }
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
94
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
95 r = STRTOD (nptr, endptr);
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
96
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
97 int saved_errno = errno;
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
98 if (uselocale (old_locale) == (locale_t)0)
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
99 /* We can't switch back to the old locale. The thread is hosed. */
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
100 abort ();
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
101 errno = saved_errno;
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
102
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
103 # endif
b716418da8b9 c-strtod, c-strtold: Use the bug fixes for strtod, strtold.
Bruno Haible <bruno@clisp.org>
parents: 40057
diff changeset
104
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
105 #else
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
106
5223
eceaa40c3485 Undo previous change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5221
diff changeset
107 char *saved_locale = setlocale (LC_NUMERIC, NULL);
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
108
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
109 if (saved_locale)
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
110 {
11075
ab7203b2967c Change c_strtod, c_strtold to no longer call xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 11074
diff changeset
111 saved_locale = strdup (saved_locale);
ab7203b2967c Change c_strtod, c_strtold to no longer call xalloc_die().
Bruno Haible <bruno@clisp.org>
parents: 11074
diff changeset
112 if (saved_locale == NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11082
diff changeset
113 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11082
diff changeset
114 if (endptr)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11082
diff changeset
115 *endptr = (char *) nptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11082
diff changeset
116 return 0; /* errno is set here */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11082
diff changeset
117 }
5223
eceaa40c3485 Undo previous change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5221
diff changeset
118 setlocale (LC_NUMERIC, "C");
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
119 }
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
120
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
121 r = STRTOD (nptr, endptr);
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
122
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
123 if (saved_locale)
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
124 {
11070
42d47e0b9b24 Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
125 int saved_errno = errno;
42d47e0b9b24 Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
126
5223
eceaa40c3485 Undo previous change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5221
diff changeset
127 setlocale (LC_NUMERIC, saved_locale);
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
128 free (saved_locale);
11070
42d47e0b9b24 Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
129 errno = saved_errno;
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
130 }
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
131
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
132 #endif
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
133
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
134 return r;
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
135 }