Fix handling of grouping characters when validating doubles

In QDoubleSpinBoxPrivate::validateAndInterpret() some code still
assumed checking one entry in a QString is enough (it isn't - the
grouping separator may be a surrogate pair) and nothing considered the
case of an empty grouping separator (as can arise if the user sets
that in their system configuration, as a brute-force way to suppress
digit-grouping). Failure to consider this case failed an assertion on
dereferencing the first character of the separator.

Handle the case of empty separator and suppress tests that try to
match a separator, when there is no separator.

Pick-to: 6.2
Fixes: QTBUG-96734
Change-Id: I63d3205ef8656a2cd2c0d855a25e712ad66e2429
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Edward Welbourne 2021-10-04 18:30:11 +02:00
parent f644a12212
commit c1281c306c

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
@ -1281,9 +1281,10 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
const bool minus = min <= 0;
const QString group(locale.groupSeparator());
const uint groupUcs = (group.size() > 1 && group.at(0).isHighSurrogate()
? QChar::surrogateToUcs4(group.at(0), group.at(1))
: group.at(0).unicode());
const uint groupUcs = (group.isEmpty() ? 0 :
(group.size() > 1 && group.at(0).isHighSurrogate()
? QChar::surrogateToUcs4(group.at(0), group.at(1))
: group.at(0).unicode()));
switch (len) {
case 0:
state = max != min ? QValidator::Intermediate : QValidator::Invalid;
@ -1306,7 +1307,7 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
default: break;
}
if (copy.at(0) == locale.groupSeparator()) {
if (groupUcs && copy.startsWith(group)) {
QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
state = QValidator::Invalid;
goto end;
@ -1322,8 +1323,9 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
state = QValidator::Invalid;
goto end;
}
for (int i=dec + 1; i<copy.size(); ++i) {
if (copy.at(i).isSpace() || copy.at(i) == locale.groupSeparator()) {
for (int i = dec + 1; i < copy.size(); ++i) {
if (copy.at(i).isSpace()
|| (groupUcs && QStringView{copy}.sliced(i).startsWith(group))) {
QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
state = QValidator::Invalid;
goto end;
@ -1331,10 +1333,11 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
}
} else {
const QChar last = copy.back();
const bool groupEnd = copy.endsWith(group);
const bool groupEnd = groupUcs && copy.endsWith(group);
const QStringView head(copy.constData(), groupEnd ? len - group.size() : len - 1);
const QChar secondLast = head.back();
if ((groupEnd || last.isSpace()) && (head.endsWith(group) || secondLast.isSpace())) {
if ((groupEnd || last.isSpace())
&& ((groupUcs && head.endsWith(group)) || secondLast.isSpace())) {
state = QValidator::Invalid;
QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
goto end;
@ -1353,7 +1356,7 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
if (!ok) {
if (QChar::isPrint(groupUcs)) {
if (max < 1000 && min > -1000 && copy.contains(group)) {
if (max < 1000 && min > -1000 && groupUcs && copy.contains(group)) {
state = QValidator::Invalid;
QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
goto end;
@ -1361,7 +1364,7 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
const int len = copy.size();
for (int i = 0; i < len - 1;) {
if (QStringView(copy).mid(i).startsWith(group)) {
if (groupUcs && QStringView{copy}.sliced(i).startsWith(group)) {
if (QStringView(copy).mid(i + group.size()).startsWith(group)) {
QSBDEBUG() << __FILE__ << __LINE__<< "state is set to Invalid";
state = QValidator::Invalid;
@ -1374,7 +1377,8 @@ QVariant QDoubleSpinBoxPrivate::validateAndInterpret(QString &input, int &pos,
}
QString copy2 = copy;
copy2.remove(group);
if (groupUcs)
copy2.remove(group);
num = locale.toDouble(copy2, &ok);
QSBDEBUG() << group << num << copy2 << ok;