summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2024-03-01 22:02:47 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2024-03-05 17:05:41 +0000
commit4efc8dd8d026d7b31d0e5411d687d28a22fb6671 (patch)
tree113af19932ac7fada814be42edff2cdb3d71604c
parent68cf23f2f8ac4db63cce2068e7f9be548515c657 (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.5 6.2 5.15 Fixes: QTBUG-40561 Change-Id: I593011b63c39f2310204d97ec61da7cf78a0fc14 Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> (cherry picked from commit de0230467c1f658232b101a99e62d68992173592) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit 975fc3da70ee83410876a205a2ebfbf25dd27e30)
-rw-r--r--src/xml/dom/qdom.cpp4
-rw-r--r--src/xml/dom/qdom.h3
-rw-r--r--tests/auto/xml/dom/qdom/CMakeLists.txt1
-rw-r--r--tests/auto/xml/dom/qdom/tst_qdom.cpp16
4 files changed, 24 insertions, 0 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp
index 6959b634be..721981fd80 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 7333b60774..2557a28de1 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 29de35f40d..241697325c 100644
--- a/tests/auto/xml/dom/qdom/CMakeLists.txt
+++ b/tests/auto/xml/dom/qdom/CMakeLists.txt
@@ -18,5 +18,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 4cf48a9f4b..337491eb01 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"