diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2024-03-01 22:02:47 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2024-03-02 21:22:53 +0100 |
commit | de0230467c1f658232b101a99e62d68992173592 (patch) | |
tree | 6695d79a9e9300e01261fa763644007d0c6fe6cb | |
parent | 398a51686eda225f222d9df7ce56898c18ecdaa1 (diff) |
QtXml: fix leak in QDomText::splitText
QDomText::splitText() needs to unref() the newly created QDomText
instance as it does not use it by itself
Pick-to: 6.7 6.6 6.5 6.2 5.15
Fixes: QTBUG-40561
Change-Id: I593011b63c39f2310204d97ec61da7cf78a0fc14
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
-rw-r--r-- | src/xml/dom/qdom.cpp | 4 | ||||
-rw-r--r-- | src/xml/dom/qdom.h | 3 | ||||
-rw-r--r-- | tests/auto/xml/dom/qdom/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/xml/dom/qdom/tst_qdom.cpp | 16 |
4 files changed, 24 insertions, 0 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp index 6e5e59bd99..2ac044004e 100644 --- a/src/xml/dom/qdom.cpp +++ b/src/xml/dom/qdom.cpp @@ -4671,6 +4671,10 @@ QDomTextPrivate* QDomTextPrivate::splitText(int offset) value.truncate(offset); parent()->insertAfter(t, this); + Q_ASSERT(t->ref.loadRelaxed() == 2); + + // We are not interested in this node + t->ref.deref(); return t; } diff --git a/src/xml/dom/qdom.h b/src/xml/dom/qdom.h index 4f01f92353..a3900609c4 100644 --- a/src/xml/dom/qdom.h +++ b/src/xml/dom/qdom.h @@ -7,6 +7,8 @@ #include <QtXml/qtxmlglobal.h> #include <QtCore/qstring.h> +class tst_QDom; + QT_BEGIN_NAMESPACE @@ -201,6 +203,7 @@ protected: QDomNode(QDomNodePrivate*); private: + friend class ::tst_QDom; friend class QDomDocument; friend class QDomDocumentType; friend class QDomNodeList; diff --git a/tests/auto/xml/dom/qdom/CMakeLists.txt b/tests/auto/xml/dom/qdom/CMakeLists.txt index b08701a640..4eea988cc3 100644 --- a/tests/auto/xml/dom/qdom/CMakeLists.txt +++ b/tests/auto/xml/dom/qdom/CMakeLists.txt @@ -24,5 +24,6 @@ qt_internal_add_test(tst_qdom tst_qdom.cpp LIBRARIES Qt::Xml + Qt::XmlPrivate TESTDATA ${test_data} ) diff --git a/tests/auto/xml/dom/qdom/tst_qdom.cpp b/tests/auto/xml/dom/qdom/tst_qdom.cpp index 6ab584aea6..b73dbc49e2 100644 --- a/tests/auto/xml/dom/qdom/tst_qdom.cpp +++ b/tests/auto/xml/dom/qdom/tst_qdom.cpp @@ -15,6 +15,7 @@ #include <QtXml> #include <QVariant> #include <cmath> +#include <QtXml/private/qdom_p.h> QT_FORWARD_DECLARE_CLASS(QDomDocument) QT_FORWARD_DECLARE_CLASS(QDomNode) @@ -106,6 +107,7 @@ private slots: void DTDInternalSubset_data() const; void QTBUG49113_dontCrashWithNegativeIndex() const; void standalone(); + void splitTextLeakMemory() const; void cleanupTestCase() const; @@ -2315,5 +2317,19 @@ void tst_QDom::DTDInternalSubset_data() const << internalSubset0; } +void tst_QDom::splitTextLeakMemory() const +{ + QDomDocument doc; + QDomElement top = doc.createElement("top"); + QDomText text = doc.createTextNode("abcdefgh"); + top.appendChild(text); + QDomText end = text.splitText(2); + QCOMPARE(text.data(), "ab"_L1); + QCOMPARE(end.data(), "cdefgh"_L1); + // only the parent node and the document have a reference on the nodes + QCOMPARE(text.impl->ref.loadRelaxed(), 2); + QCOMPARE(end.impl->ref.loadRelaxed(), 2); +} + QTEST_MAIN(tst_QDom) #include "tst_qdom.moc" |