Use NoLocalStrtod for international compliance

Recieved errors pasing json files with decimal floats from European colleagues
pull/5489/head
Aaron Bray 6 years ago
parent c7352b39f8
commit 8c9e0ff0f7
  1. 7
      src/google/protobuf/stubs/strutil.cc

@ -41,6 +41,7 @@
#include <limits>
#include <google/protobuf/stubs/stl_util.h>
#include <google/protobuf/io/strtod.h>
#ifdef _WIN32
// MSVC has only _snprintf, not snprintf.
@ -1286,7 +1287,7 @@ char* DoubleToBuffer(double value, char* buffer) {
// of a double. This long double may have extra bits that make it compare
// unequal to "value" even though it would be exactly equal if it were
// truncated to a double.
volatile double parsed_value = strtod(buffer, nullptr);
volatile double parsed_value = io::NoLocaleStrtod(buffer, nullptr);
if (parsed_value != value) {
int snprintf_result =
snprintf(buffer, kDoubleToBufferSize, "%.*g", DBL_DIG+2, value);
@ -1338,7 +1339,7 @@ bool safe_strtof(const char* str, float* value) {
char* endptr;
errno = 0; // errno only gets set on errors
#if defined(_WIN32) || defined (__hpux) // has no strtof()
*value = strtod(str, &endptr);
*value = io::NoLocaleStrtod(str, &endptr);
#else
*value = strtof(str, &endptr);
#endif
@ -1347,7 +1348,7 @@ bool safe_strtof(const char* str, float* value) {
bool safe_strtod(const char* str, double* value) {
char* endptr;
*value = strtod(str, &endptr);
*value = io::NoLocaleStrtod(str, &endptr);
if (endptr != str) {
while (ascii_isspace(*endptr)) ++endptr;
}

Loading…
Cancel
Save