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>
This commit is contained in:
Edward Welbourne 2021-07-29 18:07:36 +02:00
parent 73fabadcee
commit 5674610d3f

View File

@ -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;