From 9d6a4e70946105e6af2ff4dde281297627881928 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 4 Jan 2016 15:14:21 +0100 Subject: QLocale: Accept trailing junk in qstrtod() qstrtod() used to accept trailing junk until libdouble-conversion was introduced and we need this behavior in order to implement EcmaScript's parseFloat() correctly. The QString and QByteArray methods should not accept trailing junk, though. Task-number: QTBUG-50131 Change-Id: Ide922da0d65b2576be2c9f47f6053057eff77994 Reviewed-by: Lars Knoll --- src/corelib/tools/qlocale_tools.cpp | 18 +++++++++++------- src/corelib/tools/qlocale_tools_p.h | 8 +++++++- 2 files changed, 18 insertions(+), 8 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/tools/qlocale_tools.cpp b/src/corelib/tools/qlocale_tools.cpp index f766a301c5..44704b3575 100644 --- a/src/corelib/tools/qlocale_tools.cpp +++ b/src/corelib/tools/qlocale_tools.cpp @@ -271,7 +271,8 @@ void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char * --length; } -double asciiToDouble(const char *num, int numLen, bool &ok, int &processed) +double asciiToDouble(const char *num, int numLen, bool &ok, int &processed, + TrailingJunkMode trailingJunkMode) { if (*num == '\0') { ok = false; @@ -308,7 +309,9 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed) double d = 0.0; #if !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED) - int conv_flags = double_conversion::StringToDoubleConverter::NO_FLAGS; + int conv_flags = (trailingJunkMode == TrailingJunkAllowed) ? + double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK : + double_conversion::StringToDoubleConverter::NO_FLAGS; double_conversion::StringToDoubleConverter conv(conv_flags, 0.0, qt_snan(), 0, 0); d = conv.StringToDouble(num, numLen, &processed); @@ -327,7 +330,7 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed) if (qDoubleSscanf(num, QT_CLOCALE, "%lf%n", &d, &processed) < 1) processed = 0; - if (processed != numLen || qIsNaN(d)) { + if ((trailingJunkMode == TrailingJunkProhibited && processed != numLen) || qIsNaN(d)) { // Implementation defined nan symbol or garbage found. We don't accept it. processed = 0; ok = false; @@ -339,7 +342,7 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed) // We assume that any infinity symbol has to contain a character that cannot be part of a // "normal" number (that is 0-9, ., -, +, e). ok = false; - for (int i = 0; i < numLen; ++i) { + for (int i = 0; i < processed; ++i) { char c = num[i]; if ((c < '0' || c > '9') && c != '.' && c != '-' && c != '+' && c != 'e') { // Garbage found @@ -351,11 +354,12 @@ double asciiToDouble(const char *num, int numLen, bool &ok, int &processed) } #endif // !defined(QT_NO_DOUBLECONVERSION) && !defined(QT_BOOTSTRAPPED) - Q_ASSERT(processed == numLen); // Otherwise we would have gotten NaN or sorted it out above. + // Otherwise we would have gotten NaN or sorted it out above. + Q_ASSERT(trailingJunkMode == TrailingJunkAllowed || processed == numLen); // Check if underflow has occurred. if (isZero(d)) { - for (int i = 0; i < numLen; ++i) { + for (int i = 0; i < processed; ++i) { if (num[i] >= '1' && num[i] <= '9') { // if a digit before any 'e' is not 0, then a non-zero number was intended. ok = false; @@ -529,7 +533,7 @@ double qstrtod(const char *s00, const char **se, bool *ok) bool nonNullOk = false; int len = static_cast(strlen(s00)); Q_ASSERT(len >= 0); - double d = asciiToDouble(s00, len, nonNullOk, processed); + double d = asciiToDouble(s00, len, nonNullOk, processed, TrailingJunkAllowed); if (se) *se = s00 + processed; if (ok) diff --git a/src/corelib/tools/qlocale_tools_p.h b/src/corelib/tools/qlocale_tools_p.h index 3a8036c642..f28b3dffcd 100644 --- a/src/corelib/tools/qlocale_tools_p.h +++ b/src/corelib/tools/qlocale_tools_p.h @@ -66,7 +66,13 @@ QT_BEGIN_NAMESPACE -double asciiToDouble(const char *num, int numLen, bool &ok, int &processed); +enum TrailingJunkMode { + TrailingJunkProhibited, + TrailingJunkAllowed +}; + +double asciiToDouble(const char *num, int numLen, bool &ok, int &processed, + TrailingJunkMode trailingJunkMode = TrailingJunkProhibited); void doubleToAscii(double d, QLocaleData::DoubleForm form, int precision, char *buf, int bufSize, bool &sign, int &length, int &decpt); -- cgit v1.2.3