Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Fix a crash in KisLibraryResourcePointer
Dmitry Kazakov84ac950a931a
diff --git a/libs/flake/tests/CMakeLists.txt b/libs/flake/tests/CMakeLists.txtindex e1855b6dc8..ed8317b464 100644--- a/libs/flake/tests/CMakeLists.txt+++ b/libs/flake/tests/CMakeLists.txt@@ -28,6 +28,7 @@ kis_add_tests(TestKoMarkerCollection.cppTestSvgSavingContext.cppTestXsimdPainting.cpp+ TestFontLibraryResourceUtils.cppLINK_LIBRARIES kritaflake kritatestsdkNAME_PREFIX "libs-flake-"diff --git a/libs/flake/tests/TestFontLibraryResourceUtils.cpp b/libs/flake/tests/TestFontLibraryResourceUtils.cppnew file mode 100644index 0000000000..f8041b3854--- /dev/null+++ b/libs/flake/tests/TestFontLibraryResourceUtils.cpp@@ -0,0 +1,142 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "TestFontLibraryResourceUtils.h"++#include <kis_debug.h>+#include <kis_global.h>+#include <KoFontLibraryResourceUtils.h>++struct LibraryResource {+ LibraryResource() {+ }++ ~LibraryResource() {+ KIS_ASSERT(refCounter == 0);+ }++ int refCounter = 0;+};++LibraryResource* allocLibraryResource() {+ LibraryResource *res = new LibraryResource();+ res->refCounter++;+ return res;+}++void destroyLibraryResource(LibraryResource *res) {+ KIS_ASSERT(res);+ KIS_ASSERT(res->refCounter == 1);+ res->refCounter--;+ delete res;+}++int failingDestroyLibraryResource(LibraryResource *res) {+ KIS_ASSERT(res->refCounter == 1);+ res->refCounter--;+ delete res;++ // always report that we failed to destroy+ return -1;+}++++using LibraryResourceSP = KisLibraryResourcePointer<LibraryResource, destroyLibraryResource>;+++void TestFontLibraryResourceUtils::initTestCase()+{+ qputenv("KRITA_NO_ASSERT_MSG", "1");+}++void TestFontLibraryResourceUtils::testCreation()+{+ LibraryResourceSP res(allocLibraryResource());+ QVERIFY(res.data());+ QCOMPARE(res->refCounter, 1);+}++void TestFontLibraryResourceUtils::testCopy()+{+ LibraryResourceSP res(allocLibraryResource());+ QVERIFY(res.data());+ QCOMPARE(res->refCounter, 1);++ LibraryResourceSP res2(res);+ QVERIFY(res2.data());+ QCOMPARE(res2->refCounter, 1);+}++void TestFontLibraryResourceUtils::testCopyAssignment()+{+ LibraryResourceSP res(allocLibraryResource());+ QVERIFY(res.data());+ QCOMPARE(res->refCounter, 1);++ LibraryResourceSP res2(allocLibraryResource());+ QVERIFY(res2.data());+ QCOMPARE(res2->refCounter, 1);++ res = res2;++ QVERIFY(res.data());+ QCOMPARE(res->refCounter, 1);++ QVERIFY(res2.data());+ QCOMPARE(res2->refCounter, 1);+}++void TestFontLibraryResourceUtils::testMove()+{+ LibraryResourceSP res(allocLibraryResource());+ QVERIFY(res.data());+ QCOMPARE(res->refCounter, 1);++ LibraryResourceSP res2(std::move(res));+ QVERIFY(res2.data());+ QCOMPARE(res2->refCounter, 1);+}++void TestFontLibraryResourceUtils::testReset()+{+ LibraryResourceSP res;+ res.reset(allocLibraryResource());+ QVERIFY(res.data());+ QCOMPARE(res->refCounter, 1);++ res.reset();++}++void TestFontLibraryResourceUtils::testAssignNull()+{+ LibraryResourceSP res;++ LibraryResourceSP res2 = res;++ LibraryResourceSP res3(nullptr);++}++void TestFontLibraryResourceUtils::testFailedDestruction()+{+ using FailingLibraryResourceSP =+ KisLibraryResourcePointerWithSanityCheck<LibraryResource,+ failingDestroyLibraryResource>;++ {+ qInfo() << "# Null resource destruction should succeed:";+ FailingLibraryResourceSP res;+ }++ {+ qInfo() << "# Non-null resource destruction should succeed:";+ FailingLibraryResourceSP res(allocLibraryResource());+ }+}++SIMPLE_TEST_MAIN(TestFontLibraryResourceUtils)diff --git a/libs/flake/tests/TestFontLibraryResourceUtils.h b/libs/flake/tests/TestFontLibraryResourceUtils.hnew file mode 100644index 0000000000..972b521ef4--- /dev/null+++ b/libs/flake/tests/TestFontLibraryResourceUtils.h@@ -0,0 +1,27 @@+/*+ * SPDX-FileCopyrightText: 2017 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef TESTLIBRARYRESOURCEUTILS_H+#define TESTLIBRARYRESOURCEUTILS_H++#include <QObject>+#include <simpletest.h>++class TestFontLibraryResourceUtils : public QObject+{+ Q_OBJECT+private Q_SLOTS:+ void initTestCase();+ void testCreation();+ void testCopy();+ void testCopyAssignment();+ void testMove();+ void testReset();+ void testAssignNull();+ void testFailedDestruction();+};++#endif // TESTLIBRARYRESOURCEUTILS_Hdiff --git a/libs/flake/text/KoFontLibraryResourceUtils.h b/libs/flake/text/KoFontLibraryResourceUtils.hindex 47280035f6..0ba8919103 100644--- a/libs/flake/text/KoFontLibraryResourceUtils.h+++ b/libs/flake/text/KoFontLibraryResourceUtils.h@@ -18,15 +18,24 @@#include <QSharedPointer>-// Helper to clean up only if the pointer is non-null.-template<typename T, void (*d)(T *)>-inline void deleter(T *ptr)+#include <kis_debug.h>++namespace detail {++template <typename T, int (*P)(T *)>+void checkCStyleResultWrapper(T *ptr){- if (ptr) {- d(ptr);+ const int result = P(ptr);+ if (result != 0) {+ qWarning() << "WARNING: failed to release a library resource";+#ifdef __GNUC__+ qWarning() << " source:" << __PRETTY_FUNCTION__;+#endif}}+}+/*** Shared pointer that holds a standard allocated resource.* We use a wrapper because by C++ standards it calls the deleter@@ -37,57 +46,56 @@ template<typename T, void (*P)(T *)>struct KisLibraryResourcePointer : private QSharedPointer<T> {public:KisLibraryResourcePointer()- : QSharedPointer<T>(nullptr, deleter<T, P>)+ : QSharedPointer<T>(nullptr){}KisLibraryResourcePointer(T *ptr)- : QSharedPointer<T>(ptr, deleter<T, P>)+ : QSharedPointer<T>(ptr, ptr ? P : &KisLibraryResourcePointer::noDestroy){}using QSharedPointer<T>::operator->;- using QSharedPointer<T>::reset;++ void reset(T *ptr) {+ QSharedPointer<T>::reset(ptr, ptr ? P : &KisLibraryResourcePointer::noDestroy);+ }++ void reset() {+ QSharedPointer<T>::reset();+ }auto data() const{return this->get();}++private:+ static void noDestroy(T *ptr) {+ Q_UNUSED(ptr);+ }};/**- * Shared pointer that holds a standard allocated resource.- * The only difference is that the deleter supports being called- * for null pointer, so we can call it directly without any wrapper.+ * A special version of a class for resource types whose destructor+ * returns integer value reporting if destruction was successful or+ * now. We wrap this destructor to spit a warning in case of a trouble+ * to release the type.*/template<typename T, int (*P)(T *)>-struct KisLibraryResourcePointerAllowsNull : private QSharedPointer<T> {-public:- KisLibraryResourcePointerAllowsNull()- : QSharedPointer<T>(nullptr, P)- {- }-- KisLibraryResourcePointerAllowsNull(T *ptr)- : QSharedPointer<T>(ptr, P)- {- }-- using QSharedPointer<T>::operator->;- using QSharedPointer<T>::reset;+using KisLibraryResourcePointerWithSanityCheck = KisLibraryResourcePointer<T, detail::checkCStyleResultWrapper<T, P>>;- auto data() const- {- return this->get();- }-};using FcConfigSP = KisLibraryResourcePointer<FcConfig, FcConfigDestroy>;using FcCharSetSP = KisLibraryResourcePointer<FcCharSet, FcCharSetDestroy>;using FcPatternSP = KisLibraryResourcePointer<FcPattern, FcPatternDestroy>;using FcFontSetSP = KisLibraryResourcePointer<FcFontSet, FcFontSetDestroy>;-using FT_LibrarySP = KisLibraryResourcePointerAllowsNull<std::remove_pointer_t<FT_Library>, FT_Done_FreeType>;-using FT_FaceSP = KisLibraryResourcePointerAllowsNull<std::remove_pointer_t<FT_Face>, FT_Done_Face>;++using FT_LibrarySP =+ KisLibraryResourcePointerWithSanityCheck<std::remove_pointer_t<FT_Library>,+ FT_Done_FreeType>;+using FT_FaceSP = KisLibraryResourcePointerWithSanityCheck<std::remove_pointer_t<FT_Face>,+ FT_Done_Face>;using hb_font_t_sp = KisLibraryResourcePointer<hb_font_t, hb_font_destroy>;