1 /*
2  * Copyright (c) 2009-2022, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/lex/strtod.h"
29 
30 #include <stdlib.h>
31 #include <string.h>
32 
33 // Must be last.
34 #include "upb/port/def.inc"
35 
36 // Determine the locale-specific radix character by calling sprintf() to print
37 // the number 1.5, then stripping off the digits.  As far as I can tell, this
38 // is the only portable, thread-safe way to get the C library to divulge the
39 // locale's radix character.  No, localeconv() is NOT thread-safe.
40 
GetLocaleRadix(char * data,size_t capacity)41 static int GetLocaleRadix(char *data, size_t capacity) {
42   char temp[16];
43   const int size = snprintf(temp, sizeof(temp), "%.1f", 1.5);
44   UPB_ASSERT(temp[0] == '1');
45   UPB_ASSERT(temp[size - 1] == '5');
46   UPB_ASSERT(size < capacity);
47   temp[size - 1] = '\0';
48   strcpy(data, temp + 1);
49   return size - 2;
50 }
51 
52 // Populates a string identical to *input except that the character pointed to
53 // by pos (which should be '.') is replaced with the locale-specific radix.
54 
LocalizeRadix(const char * input,const char * pos,char * output)55 static void LocalizeRadix(const char *input, const char *pos, char *output) {
56   const int len1 = pos - input;
57 
58   char radix[8];
59   const int len2 = GetLocaleRadix(radix, sizeof(radix));
60 
61   memcpy(output, input, len1);
62   memcpy(output + len1, radix, len2);
63   strcpy(output + len1 + len2, input + len1 + 1);
64 }
65 
_upb_NoLocaleStrtod(const char * str,char ** endptr)66 double _upb_NoLocaleStrtod(const char *str, char **endptr) {
67   // We cannot simply set the locale to "C" temporarily with setlocale()
68   // as this is not thread-safe.  Instead, we try to parse in the current
69   // locale first.  If parsing stops at a '.' character, then this is a
70   // pretty good hint that we're actually in some other locale in which
71   // '.' is not the radix character.
72 
73   char *temp_endptr;
74   double result = strtod(str, &temp_endptr);
75   if (endptr != NULL) *endptr = temp_endptr;
76   if (*temp_endptr != '.') return result;
77 
78   // Parsing halted on a '.'.  Perhaps we're in a different locale?  Let's
79   // try to replace the '.' with a locale-specific radix character and
80   // try again.
81 
82   char localized[80];
83   LocalizeRadix(str, temp_endptr, localized);
84   char *localized_endptr;
85   result = strtod(localized, &localized_endptr);
86   if ((localized_endptr - &localized[0]) > (temp_endptr - str)) {
87     // This attempt got further, so replacing the decimal must have helped.
88     // Update endptr to point at the right location.
89     if (endptr != NULL) {
90       // size_diff is non-zero if the localized radix has multiple bytes.
91       int size_diff = strlen(localized) - strlen(str);
92       *endptr = (char *)str + (localized_endptr - &localized[0] - size_diff);
93     }
94   }
95 
96   return result;
97 }
98