From 9ecd529d05685b9fa4d5adc40bc1d0168aed8966 Mon Sep 17 00:00:00 2001 From: Thomas Fischer Date: Sat, 31 Dec 2022 00:00:04 +0100 Subject: [PATCH] Fixing crash when opening .bib file 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 | 36 ++++++++++++++++++++ src/test/kbibtexguitest.cpp | 66 +++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/test/kbibtexguitest.cpp diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 72463e0..8eaba5d 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -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 index 0000000..e8ab244 --- /dev/null +++ b/src/test/kbibtexguitest.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2022 by Thomas Fischer * + * * + * 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 . * + ***************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +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(new Entry(Entry::etArticle, QStringLiteral("kirsop2005accessrelitdevcountries"))); + bibTeXfile->append(entry); + entry->insert(Entry::ftTitle, Value() << QSharedPointer(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" -- 2.30.2