From 5674610d3f4e9b2fe08e722754e2f6d6dd1b12a2 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Jul 2021 18:07:36 +0200 Subject: 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 --- src/corelib/text/qlocale.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') 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; -- cgit v1.2.3