From 149043a24273e5f02ed921e33c256567156fa95a Mon Sep 17 00:00:00 2001
From: Aqua-sama <aqua@iserlohn-fortress.net>
Date: Thu, 28 May 2020 22:05:45 +0300
Subject: Fix segfault in release build

When creating a WebView out of a Session struct, fall back to the
default profile if one is not set, or a profile with that ID is not
found.

- Update PKGBUILD
---
 linux/makepkg/PKGBUILD            | 36 ++++++++++++++----------------------
 meson.build                       |  7 +------
 src/subwindow/subwindow.cpp       |  3 ---
 src/subwindow/tabwidget.cpp       | 16 +++++++---------
 src/webengine/webprofile.cpp      |  2 --
 src/webengine/webprofilemanager.h | 13 +++++++------
 src/webengine/webview.cpp         | 14 ++++----------
 7 files changed, 33 insertions(+), 58 deletions(-)

diff --git a/linux/makepkg/PKGBUILD b/linux/makepkg/PKGBUILD
index 5e27542..5bcf684 100644
--- a/linux/makepkg/PKGBUILD
+++ b/linux/makepkg/PKGBUILD
@@ -19,20 +19,15 @@ install="smolbote.install"
 arch=('x86_64' 'aarch64')
 license=('GPL3')
 
-depends=('qt5-webengine>=5.11.0' 'spdlog')
+depends=('qt5-svg' 'qt5-webengine>=5.11.0' 'spdlog')
 makedepends=('git' 'meson' 'python-kconfiglib' 'openssl' 'qt5-tools' 'scdoc' 'catch2')
 if [ $_enableBreakpad == "1" ]; then
   makedepends+=('breakpad-git')
 fi
 optdepends=('firejail: launch a sandboxed instance')
 
-# this isn't a hard requirement, simply a workaround as the build script
-# sets some additional hardening flags that the default makepkg.conf
-# will turn down
-options=(!buildflags)
-
 # use git+file:///path/to/your/repo to build from a local repo
-source=("git+https://library.iserlohn-fortress.net/aqua/smolbote.git"
+source=("git+https://neueland.iserlohn-fortress.net/cgit/smolbote"
         "https://neueland.iserlohn-fortress.net/releases/SingleApplication-3.1.1a.tar.xz"{,.sig}
         "https://neueland.iserlohn-fortress.net/releases/args.hxx-6.2.2.tar.xz"{,.sig})
 
@@ -48,6 +43,9 @@ prepare() {
     mkdir "$srcdir/smolbote/subprojects/packagecache/"
     ln -s "$srcdir/SingleApplication-3.1.1a" "$srcdir/smolbote/subprojects/"
     ln -s "$srcdir/args.hxx-6.2.2" "$srcdir/smolbote/subprojects/"
+
+    cd $srcdir/smolbote
+    KCONFIG_CONFIG=linux/.config alldefconfig
 }
 
 pkgver() {
@@ -58,11 +56,8 @@ pkgver() {
 
 build() {
     mkdir -p $srcdir/build
-    cd $srcdir/smolbote
-    KCONFIG_CONFIG=linux/.config alldefconfig
 
-    # For a list of configureable options, check smolbote/meson_options.txt, or
-    # run `meson configure` in $srcdir/build
+    # For a list of configureable options, check smolbote/meson_options.txt, or run `meson configure` in $srcdir/build
 
     # --wrap-mode=nodownload - disable meson from downloading dependency wraps. This will cause it to fail if makedepends are not found by pkg-config or cmake.
     # --wrap-mode=nofallback - disable downloads as a fallback too. Implies --wrap-mode=nodownload
@@ -71,11 +66,11 @@ build() {
     # --auto-features=disabled - features should be explicitly enabled
     # b_pie: Build executables as position independent
     # b_lto: Use link time optimization
-    meson --buildtype=release --wrap-mode=nodownload \
-        --prefix=$_prefix --auto-features=disabled \
+    meson setup \
+        --buildtype=plain --prefix=$_prefix --wrap-mode=nodownload --auto-features=disabled \
         -Db_pie=true -Ddefault_library=static \
-        -Dmanpage=enabled \
-        $srcdir/build
+        -Dmanpage=true \
+        $srcdir/smolbote $srcdir/build
 
     if [ $_enableBreakpad == "1" ]; then
         msg2 "Enabling crashhandler"
@@ -83,16 +78,13 @@ build() {
         KCONFIG_CONFIG=linux/.config setconfig USEBREAKPAD=y
     fi
 
-    # Run menuconfig
-    #KCONFIG_CONFIG=linux/.config menuconfig
-
     # Build
-    ninja -C $srcdir/build "$MAKEFLAGS"
+    ninja -C $srcdir/build
 }
 
-check() {
-    ninja -C $srcdir/build test
-}
+#check() {
+#    ninja -C $srcdir/build test
+#}
 
 package() {
     # Install
diff --git a/meson.build b/meson.build
index bc72ff3..56e50e7 100644
--- a/meson.build
+++ b/meson.build
@@ -74,12 +74,7 @@ add_project_arguments(cxx.get_supported_arguments([
   #'-Weffc++',
 ]), language: 'cpp')
 
-if get_option('buildtype') == 'release'
-  add_project_arguments(cxx.get_supported_arguments([
-    '-flto=4',
-  ]), language: 'cpp')
-endif
-
+# Dependencies
 mod_qt5 = import('qt5')
 dep_qt5 = dependency('qt5',
   modules: [ 'Core', 'Network', 'Widgets', 'Svg', 'WebEngine', 'WebEngineWidgets', 'Concurrent' ],
diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp
index 6e9a713..a2d6138 100644
--- a/src/subwindow/subwindow.cpp
+++ b/src/subwindow/subwindow.cpp
@@ -57,7 +57,6 @@ SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags flags)
             close();
         } else {
             auto *view = dynamic_cast<WebView *>(tabWidget->widget(index));
-            Q_CHECK_PTR(view);
 
             disconnect(titleConnection);
             disconnect(linkHoveredConnection);
@@ -67,12 +66,10 @@ SubWindow::SubWindow(QWidget *parent, Qt::WindowFlags flags)
                 this->setWindowTitle(QString("%1 :%2").arg(title, v->profile()->name()));
             });
             setWindowTitle(QString("%1 :%2").arg(view->title(), view->profile()->name()));
-
             linkHoveredConnection = connect(view->page(), &WebPage::linkHovered, this, [this](const QString &url) {
                 if(!url.isEmpty())
                     emit showStatusMessage(url, 3000);
             });
-
             emit currentViewChanged(view);
         }
     });
diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp
index 6f1e348..69f3b8a 100644
--- a/src/subwindow/tabwidget.cpp
+++ b/src/subwindow/tabwidget.cpp
@@ -42,8 +42,11 @@ TabWidget::TabWidget(SubWindow *parent)
     // when changing tabs, give focus to the widget
     // otherwise when closing tabs, the tabwidget will retain focus
     connect(this, &TabWidget::currentChanged, this, [this](int index) {
-        if(widget(index))
+        previous = current;
+        current = index;
+        /*if(widget(index)) {
             widget(index)->setFocus();
+        }*/
     });
 
     // context menu
@@ -68,12 +71,6 @@ TabWidget::TabWidget(SubWindow *parent)
             removeTab(i);
         }
     });
-
-    //
-    connect(this, &TabWidget::currentChanged, this, [this](int index) {
-        previous = current;
-        current = index;
-    });
 }
 
 TabWidget::~TabWidget()
@@ -85,7 +82,9 @@ TabWidget::~TabWidget()
 
 int TabWidget::addTab(WebView *view)
 {
-    Q_ASSERT_X(view != nullptr, "TabWidget::addTab", "Tried to add null view");
+    if(view == nullptr) {
+        return -1;
+    }
 
     const int idx = QTabWidget::addTab(view, view->title());
     connect(view, &WebView::titleChanged, [this, view](const QString &title) {
@@ -100,7 +99,6 @@ int TabWidget::addTab(WebView *view)
             setTabIcon(current_idx, icon);
         }
     });
-
     tabBar()->setTabData(idx, QVariant::fromValue<SubWindow::TabData>(SubWindow::TabData{}));
     return idx;
 }
diff --git a/src/webengine/webprofile.cpp b/src/webengine/webprofile.cpp
index 719ab34..f1e71fb 100644
--- a/src/webengine/webprofile.cpp
+++ b/src/webengine/webprofile.cpp
@@ -18,12 +18,10 @@ static WebProfile *s_profile = nullptr;
 
 void WebProfile::setDefaultProfile(WebProfile *profile)
 {
-    Q_CHECK_PTR(profile);
     s_profile = profile;
 }
 WebProfile *WebProfile::defaultProfile()
 {
-    Q_CHECK_PTR(s_profile);
     return s_profile;
 }
 
diff --git a/src/webengine/webprofilemanager.h b/src/webengine/webprofilemanager.h
index 91dcaf8..a356506 100644
--- a/src/webengine/webprofilemanager.h
+++ b/src/webengine/webprofilemanager.h
@@ -41,10 +41,11 @@ public:
         }
 
         if(!profiles.contains(default_id)) {
-            Profile profile;
-            profile.settings = WebProfile::load(QString(), search, homepage, newtab);
-            profile.ptr = WebProfile::load(default_id, profile.settings, true);
-            profiles[default_id] = profile;
+            auto *settings = WebProfile::load(QString(), search, homepage, newtab);
+            profiles[default_id] = Profile{
+                .settings = settings,
+                .ptr = WebProfile::load(default_id, settings, true),
+            };
         }
         WebProfile::setDefaultProfile(profiles[default_id].ptr);
     }
@@ -73,7 +74,7 @@ public:
         }
 
         if(profile != nullptr && settings != nullptr) {
-            profiles[id] = Profile{ profile, settings, false };
+            profiles[id] = Profile{ settings, profile, false };
         }
     }
 
@@ -101,8 +102,8 @@ private:
     set_typestate(consumed) void consume() {}
 
     struct Profile {
-        WebProfile *ptr = nullptr;
         QSettings *settings = nullptr;
+        WebProfile *ptr = nullptr;
         bool selfDestruct = false;
     };
 
diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp
index 38e564a..bc52102 100644
--- a/src/webengine/webview.cpp
+++ b/src/webengine/webview.cpp
@@ -37,9 +37,7 @@ WebView::WebView(WebProfile *profile, cb_createWindow_t cb, QWidget *parent)
     : WebView(parent)
 {
     cb_createWindow = cb;
-    Q_CHECK_PTR(profile);
-    m_profile = profile;
-    setPage(new WebPage(profile, this));
+    setProfile(profile);
 }
 
 WebView::WebView(const Session::WebView &webview_data, cb_createWindow_t cb, QWidget *parent)
@@ -47,11 +45,7 @@ WebView::WebView(const Session::WebView &webview_data, cb_createWindow_t cb, QWi
 {
     cb_createWindow = cb;
     WebProfileManager profileManager;
-
-    auto *profile = profileManager.profile(webview_data.profile);
-    if(profile != nullptr) {
-        setProfile(profile);
-    }
+    setProfile(profileManager.profile(webview_data.profile));
 
     if(!webview_data.url.isEmpty())
         load(QUrl::fromUserInput(webview_data.url));
@@ -64,9 +58,9 @@ WebView::WebView(const Session::WebView &webview_data, cb_createWindow_t cb, QWi
 
 void WebView::setProfile(WebProfile *profile)
 {
-    m_profile = profile;
+    m_profile = (profile == nullptr) ? WebProfile::defaultProfile() : profile;
     const auto url = this->url();
-    setPage(new WebPage(profile, this));
+    setPage(new WebPage(m_profile, this));
     this->load(url);
 }
 
-- 
cgit v1.2.1