diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2021-10-04 18:30:11 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-10-08 08:06:14 +0000 |
commit | 46c1ca0f323d8578c7550b12cd361a8334c16a4c (patch) | |
tree | 2b0525aaca8b8615392ca53cb18a9f77e97a7575 /src | |
parent | 60230870a7f121ddc0779f1eecbaf8de6cb60cdc (diff) |
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.
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>
(cherry picked from commit c1281c306c1304374cb7ba1c341db71b584bf6c6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/widgets/widgets/qspinbox.cpp | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/widgets/widgets/qspinbox.cpp b/src/widgets/widgets/qspinbox.cpp index 6ac2e1cec2..e504d7f535 100644 --- a/src/widgets/widgets/qspinbox.cpp +++ b/src/widgets/widgets/qspinbox.cpp @@ -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; |