on xenial help sign in
protect against loading invalid window geometry
commit771bc4b6ed2060794af8e782071222621f348a1b authorLukáš Tinkl <lukas.tinkl@canonical.com> dateMon 20 Mar 2017, 16:17 (10 years ago) parent593d500
2 files changed+18−2
plugins/Utils/windowstatestorage.cpp+9−2 tests/plugins/Utils/WindowStateStorageTest.cpp+9
plugins/Utils/windowstatestorage.cpp+9−2
@@ -133,8 +133,15 @@ QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defa
133133 if (!query.first()) {
134134 return defaultValue;
135135 }
136− return QRect(query.value(QStringLiteral("x")).toInt(), query.value(QStringLiteral("y")).toInt(),
137− query.value(QStringLiteral("width")).toInt(), query.value(QStringLiteral("height")).toInt());
136+
137+ const QRect result(query.value(QStringLiteral("x")).toInt(), query.value(QStringLiteral("y")).toInt(),
138+ query.value(QStringLiteral("width")).toInt(), query.value(QStringLiteral("height")).toInt());
139+
140+ if (result.isValid()) {
141+ return result;
142+ }
143+
144+ return defaultValue;
138145}
139146
140147void WindowStateStorage::initdb()
tests/plugins/Utils/WindowStateStorageTest.cpp+9
@@ -56,6 +56,15 @@ private Q_SLOTS:
5656 QTRY_COMPARE(storage->getStage(QTest::currentTestFunction(), 0), stage);
5757 }
5858
59+ void testProtectAgainstInvalidGeometry() {
60+ const QRect geometry{10, 20, 0, 10}; // zero-width (invalid) rectangle
61+ storage->saveGeometry(QTest::currentTestFunction(), geometry);
62+ const QRect defaultGeometry{10, 20, 30, 40};
63+ const QRect loadedGeometry = storage->getGeometry(QTest::currentTestFunction(), defaultGeometry);
64+ // ensure we don't load a broken geometry, instead we fall back to the default one
65+ QCOMPARE(loadedGeometry, defaultGeometry);
66+ }
67+
5968private:
6069 WindowStateStorage * storage{nullptr};
6170};