From c1281c306c1304374cb7ba1c341db71b584bf6c6 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 4 Oct 2021 18:30:11 +0200 Subject: [PATCH] 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 Reviewed-by: Volker Hilsheimer --- src/widgets/widgets/qspinbox.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/widgets/widgets/qspinbox.cpp b/src/widgets/widgets/qspinbox.cpp index 6ac2e1cec29..e504d7f5357 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 -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;