on xenial help sign in
protect against loading invalid window geometry
771bc4bLukáš Tinkl
plugins/Utils/windowstatestorage.cpp+9−2
@@ -133,8 +133,15 @@ QRect WindowStateStorage::getGeometry(const QString &windowId, const QRect &defa
133 if (!query.first()) {
133 if (!query.first()) {
134 return defaultValue;
134 return defaultValue;
135 }
135 }
136+
136− return QRect(query.value(QStringLiteral("x")).toInt(), query.value(QStringLiteral("y")).toInt(),
137+ const QRect result(query.value(QStringLiteral("x")).toInt(), query.value(QStringLiteral("y")).toInt(),
137− query.value(QStringLiteral("width")).toInt(), query.value(QStringLiteral("height")).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;
138}
145}
139
146
140void WindowStateStorage::initdb()
147void WindowStateStorage::initdb()
tests/plugins/Utils/WindowStateStorageTest.cpp+9
@@ -56,6 +56,15 @@ private Q_SLOTS:
56 QTRY_COMPARE(storage->getStage(QTest::currentTestFunction(), 0), stage);
56 QTRY_COMPARE(storage->getStage(QTest::currentTestFunction(), 0), stage);
57 }
57 }
58
58
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+
59private:
68private:
60 WindowStateStorage * storage{nullptr};
69 WindowStateStorage * storage{nullptr};
61};
70};