summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2021-07-29 18:07:36 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2021-08-05 02:44:27 +0200
commit5674610d3f4e9b2fe08e722754e2f6d6dd1b12a2 (patch)
treed3f24963b7276bbaa802ef74fed59bd10a08dfa7 /src
parent73fabadcee71af858388fb245fccf4e96d4ead4e (diff)
Pre-check a pair of string comparisons by length
When checking whether a floating-point value is "inf" or "nan", the code actually only checked they *started* with those. Check the length first and thereby avoid the check when the string is longer in any case. This incidentally avoids tripping over any string that merely starts with "inf" or "nan" - such a string should not be able to arise here; but we still shouldn't give it the special treatment reserved for these two, were one to arise. Add an assertion to the one remaining branch that wouldn't have caught such a malformed string. Change-Id: I63828e3a99a33cf236e4d1a2e247ad832b7a00fd Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qlocale.cpp6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index 52d3938516..db8e1fe563 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -3429,7 +3429,8 @@ QString QLocaleData::doubleToString(double d, int precision, DoubleForm form,
const QString prefix = signPrefix(negative && !isZero(d), flags);
QString numStr;
- if (qstrncmp(buf.data(), "inf", 3) == 0 || qstrncmp(buf.data(), "nan", 3) == 0) {
+ if (length == 3
+ && (qstrncmp(buf.data(), "inf", 3) == 0 || qstrncmp(buf.data(), "nan", 3) == 0)) {
numStr = QString::fromLatin1(buf.data(), length);
} else { // Handle finite values
const QString zero = zeroDigit();
@@ -3437,6 +3438,9 @@ QString QLocaleData::doubleToString(double d, int precision, DoubleForm form,
if (zero == u"0") {
// No need to convert digits.
+ Q_ASSERT(std::all_of(buf.cbegin(), buf.cbegin() + length, [](char ch)
+ { return '0' <= ch && ch <= '9'; }));
+ // That check is taken care of in unicodeForDigits, below.
} else if (zero.size() == 2 && zero.at(0).isHighSurrogate()) {
const char32_t zeroUcs4 = QChar::surrogateToUcs4(zero.at(0), zero.at(1));
QString converted;