From 6985a8567a234dac310c35db41ba36e0a8767a15 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sat, 3 Feb 2018 10:53:07 +0100 Subject: [PATCH] RestoreManager: Validate all elements in session file Session file can be corrupted or have data from unsupported version inside the file. --- src/lib/app/browserwindow.cpp | 5 +++++ src/lib/session/restoremanager.cpp | 31 ++++++++++-------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/lib/app/browserwindow.cpp b/src/lib/app/browserwindow.cpp index e8dc6feb1..1d34d3458 100644 --- a/src/lib/app/browserwindow.cpp +++ b/src/lib/app/browserwindow.cpp @@ -122,6 +122,11 @@ BrowserWindow::SavedWindow::SavedWindow(BrowserWindow *window) bool BrowserWindow::SavedWindow::isValid() const { + for (const WebTab::SavedTab &tab : qAsConst(tabs)) { + if (!tab.isValid()) { + return false; + } + } return currentTab > -1; } diff --git a/src/lib/session/restoremanager.cpp b/src/lib/session/restoremanager.cpp index 8fd7aebb1..dbddd3987 100644 --- a/src/lib/session/restoremanager.cpp +++ b/src/lib/session/restoremanager.cpp @@ -26,12 +26,19 @@ static const int restoreDataVersion = 2; bool RestoreData::isValid() const { + for (const BrowserWindow::SavedWindow &window : qAsConst(windows)) { + if (!window.isValid()) { + return false; + } + } return !windows.isEmpty(); } void RestoreData::clear() { windows.clear(); + crashedSession.clear(); + closedWindows.clear(); } QDataStream &operator<<(QDataStream &stream, const RestoreData &data) @@ -155,27 +162,9 @@ static void loadVersion3(QDataStream &stream, RestoreData &data) // static bool RestoreManager::validateFile(const QString &file) { - if (!QFile::exists(file)) { - return false; - } - - QFile recoveryFile(file); - if (!recoveryFile.open(QIODevice::ReadOnly)) { - return false; - } - - QDataStream stream(&recoveryFile); - - int version; - stream >> version; - - if (version == Qz::sessionVersion || version == 0x0003 || version == (0x0003 | 0x050000)) { - int windowCount; - stream >> windowCount; - return windowCount > 0; - } - - return false; + RestoreData data; + createFromFile(file, data); + return data.isValid(); } // static