diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-09-16 16:55:17 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-09-24 14:38:40 +0200 |
commit | 06cc5d307177ebacf8e6926b9690c12703004a1a (patch) | |
tree | 6143b0eaa7318a414da86d97c87fe5bb507b85ae | |
parent | 74179a209a8ccdc771b0218f430d5a0519bc619a (diff) |
Ensure that QFont::fromString() doesn't parse empty font specifications
Fixes: QTBUG-78236
Change-Id: Iba060e7a24080cdc8f317ecb6dc616b2cd918acb
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Reviewed-by: Konstantin Shegunov <kshegunov@gmail.com>
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
-rw-r--r-- | src/gui/text/qfont.cpp | 11 | ||||
-rw-r--r-- | tests/auto/gui/text/qfont/tst_qfont.cpp | 21 |
2 files changed, 27 insertions, 5 deletions
diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 1dbb03948d..84c5be60b1 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. @@ -2098,10 +2098,11 @@ uint qHash(const QFont &font, uint seed) Q_DECL_NOTHROW */ bool QFont::fromString(const QString &descrip) { - const auto l = descrip.splitRef(QLatin1Char(',')); - - int count = l.count(); - if (!count || (count > 2 && count < 9) || count > 11) { + const QStringRef sr = QStringRef(&descrip).trimmed(); + const auto l = sr.split(QLatin1Char(',')); + const int count = l.count(); + if (!count || (count > 2 && count < 9) || count > 11 || + l.first().isEmpty()) { qWarning("QFont::fromString: Invalid description '%s'", descrip.isEmpty() ? "(empty)" : descrip.toLatin1().data()); return false; diff --git a/tests/auto/gui/text/qfont/tst_qfont.cpp b/tests/auto/gui/text/qfont/tst_qfont.cpp index 9acf877790..66f3ee0d2c 100644 --- a/tests/auto/gui/text/qfont/tst_qfont.cpp +++ b/tests/auto/gui/text/qfont/tst_qfont.cpp @@ -64,6 +64,8 @@ private slots: void defaultFamily(); void toAndFromString(); void fromStringWithoutStyleName(); + void fromDegenerateString_data(); + void fromDegenerateString(); void sharing(); void familyNameWithCommaQuote_data(); @@ -600,6 +602,25 @@ void tst_QFont::fromStringWithoutStyleName() QCOMPARE(font2.toString(), str); } +void tst_QFont::fromDegenerateString_data() +{ + QTest::addColumn<QString>("string"); + + QTest::newRow("empty") << QString(); + QTest::newRow("justAComma") << ","; + QTest::newRow("commasAndSpaces") << " , , "; + QTest::newRow("spaces") << " "; + QTest::newRow("spacesTabsAndNewlines") << " \t \n"; +} + +void tst_QFont::fromDegenerateString() +{ + QFETCH(QString, string); + QFont f; + QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".*Invalid description.*")); + QCOMPARE(f.fromString(string), false); + QCOMPARE(f, QFont()); +} void tst_QFont::sharing() { |