Fixing crash when opening .bib file
authorThomas Fischer <fischer@unix-ag.uni-kl.de>
Fri, 30 Dec 2022 23:00:04 +0000 (00:00 +0100)
committerAdrian Bunk <bunk@debian.org>
Tue, 7 Feb 2023 16:22:51 +0000 (16:22 +0000)
In certain situations, also depending on Linux distributions (varying Qt
and KDE Frameworks versions), opening a bibliography file can cause
crashes (segmentation faults) due to an invalid model or its underlying
data.

This commit changes the order of two instructions. Now first an internal
variable is properly set. Only then an update on the model is applied.

The largest part of this commit is a new test that reproducibly crashes
without the changed order of two instructions, but passes after the fix
has been applied.

This commit is a forward-port of commit 4aea6ed35b1629b3dba65a44 from
branch 'kbibtex/0.10'.

CCBUG: 433084
CCBUG: 453455

Gbp-Pq: Name 0001-Fixing-crash-when-opening-.bib-file.patch

src/test/CMakeLists.txt
src/test/kbibtexguitest.cpp [new file with mode: 0644]

index 72463e0a5c79f79481600d38dbc8bb0f8a1763be..8eaba5d4f9e1d6d122e32970df5e5c48bb0e8190 100644 (file)
@@ -37,11 +37,17 @@ set(
     kbibtexdatatest.cpp
 )
 
+set(
+    kbibtexguitest_SRCS
+    kbibtexguitest.cpp
+)
+
 if(UNITY_BUILD AND NOT WIN32) # FIXME: Unity build of programs breaks on Windows
     enable_unity_build(kbibtextest kbibtextest_SRCS)
     enable_unity_build(kbibtexnetworkingtest kbibtexnetworkingtest_SRCS)
     enable_unity_build(kbibtexiotest kbibtexiotest_SRCS)
     enable_unity_build(kbibtexdatatest kbibtexdatatest_SRCS)
+    enable_unity_build(kbibtexguitest kbibtexdatatest_SRCS)
 endif(UNITY_BUILD AND NOT WIN32)
 
 add_executable(
@@ -80,6 +86,15 @@ add_dependencies(kbibtexdatatest
     generate-kbibtex-git-info
 )
 
+add_executable(
+    kbibtexguitest
+    ${kbibtexguitest_SRCS}
+)
+
+add_dependencies(kbibtexguitest
+    generate-kbibtex-git-info
+)
+
 target_link_libraries(kbibtextest
     Qt5::Core
     KF5::KIOCore
@@ -132,10 +147,24 @@ target_include_directories(kbibtexdatatest
         ${CMAKE_BINARY_DIR}
 )
 
+target_link_libraries(kbibtexguitest
+    PRIVATE
+        Qt5::Test
+        KBibTeX::Global
+        KBibTeX::Data
+        KBibTeX::GUI
+)
+
+target_include_directories(kbibtexguitest
+    PRIVATE
+        ${CMAKE_BINARY_DIR}
+)
+
 ecm_mark_as_test(
     kbibtexnetworkingtest
     kbibtexiotest
     kbibtexdatatest
+    kbibtexguitest
 )
 
 add_test(
@@ -159,6 +188,13 @@ add_test(
     kbibtexdatatest
 )
 
+add_test(
+    NAME
+    kbibtexguitest
+    COMMAND
+    kbibtexguitest
+)
+
 if(TESTSET_DIRECTORY)
     set(
         kbibtexfilestest_SRCS
diff --git a/src/test/kbibtexguitest.cpp b/src/test/kbibtexguitest.cpp
new file mode 100644 (file)
index 0000000..e8ab244
--- /dev/null
@@ -0,0 +1,66 @@
+/***************************************************************************
+ *   Copyright (C) 2022 by Thomas Fischer <fischer@unix-ag.uni-kl.de>      *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
+ ***************************************************************************/
+
+#include <QtTest>
+
+#include <field/FieldLineEdit>
+#include <File>
+#include <Entry>
+#include <models/FileModel>
+#include <file/SortFilterFileModel>
+
+class KBibTeXGUITest : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    void initTestCase();
+
+    void sortedFilterFileModelSetSourceModel();
+
+private:
+};
+
+void KBibTeXGUITest::initTestCase()
+{
+    // nothing
+}
+
+void KBibTeXGUITest::sortedFilterFileModelSetSourceModel()
+{
+    File *bibTeXfile = new File();
+    // Kirsop, Barbara, and Leslie Chan. (2005) Transforming access to research literature for developing countries. Serials Reviews, 31(4): 246–255.
+    QSharedPointer<Entry> entry(new Entry(Entry::etArticle, QStringLiteral("kirsop2005accessrelitdevcountries")));
+    bibTeXfile->append(entry);
+    entry->insert(Entry::ftTitle, Value() << QSharedPointer<PlainText>(new PlainText(QStringLiteral("Transforming access to research literature for developing countries"))));
+    entry->insert(Entry::ftAuthor, Value() << QSharedPointer<Person>(new Person(QStringLiteral("Barbara"), QStringLiteral("Kirsop"))) << QSharedPointer<Person>(new Person(QStringLiteral("Leslie"), QStringLiteral("Chan"))));
+    entry->insert(Entry::ftYear, Value() << QSharedPointer<PlainText>(new PlainText(QStringLiteral("2005"))));
+    entry->insert(Entry::ftJournal, Value() << QSharedPointer<PlainText>(new PlainText(QStringLiteral("Serials Reviews"))));
+    entry->insert(Entry::ftVolume, Value() << QSharedPointer<PlainText>(new PlainText(QStringLiteral("31"))));
+    entry->insert(Entry::ftNumber, Value() << QSharedPointer<PlainText>(new PlainText(QStringLiteral("4"))));
+    entry->insert(Entry::ftPages, Value() << QSharedPointer<PlainText>(new PlainText(QStringLiteral("246--255"))));
+
+    QPointer<FileModel> model = new FileModel();
+    model->setBibliographyFile(bibTeXfile);
+    QPointer<SortFilterFileModel> sortFilterProxyModel = new SortFilterFileModel();
+    sortFilterProxyModel->setSourceModel(model.data());
+    QCOMPARE(sortFilterProxyModel->rowCount(), 1);
+}
+
+QTEST_MAIN(KBibTeXGUITest)
+
+#include "kbibtexguitest.moc"