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)
This commit is contained in:
Christian Ehrlicher 2024-03-01 22:02:47 +01:00 committed by Qt Cherry-pick Bot
parent 68cf23f2f8
commit 4efc8dd8d0
4 changed files with 24 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -18,5 +18,6 @@ qt_internal_add_test(tst_qdom
tst_qdom.cpp
LIBRARIES
Qt::Xml
Qt::XmlPrivate
TESTDATA ${test_data}
)

View File

@ -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"