summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSami Rosendahl <ext-sami.1.rosendahl@nokia.com>2012-01-19 10:15:08 +0200
committerThiago Macieira <thiago.macieira@intel.com>2012-04-05 15:43:40 +0200
commitceb6b7cc0b5b4b60d12e7c6b2e6cdc8530f5030f (patch)
treea531d2cac558a953330ca5bbdff5ed062d1274a7
parented2a887e21a09b1eb6890ac246efca19f7c6a19d (diff)
downloadqt4-tools-ceb6b7cc0b5b4b60d12e7c6b2e6cdc8530f5030f.tar.gz
Fix memory leak in QDomDocument DTD entity declaration handler
The created entity node's reference count needs to be decremented to 0 before it is added as a child, because appendChild will increment the reference count to correct value of 1. Also added autotest DTDEntityDecl to tst_qdom to expose the leak when executed under valgrind memcheck. There was no previous direct test case for unparsed entity declarations in DTD, only indirect coverage via regression test cloneDTD_QTBUG8398. Task-number: QTBUG-22587 Change-Id: I0380cd37f65cb5a820e6b792f47e10ae31a465ad (cherry picked from commit d8d4dc8599fb251ca201f5c3f496df1045d288c3) Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/xml/dom/qdom.cpp2
-rw-r--r--tests/auto/qdom/tst_qdom.cpp25
2 files changed, 27 insertions, 0 deletions
diff --git a/src/xml/dom/qdom.cpp b/src/xml/dom/qdom.cpp
index c0bfda7d4c..8009832a1f 100644
--- a/src/xml/dom/qdom.cpp
+++ b/src/xml/dom/qdom.cpp
@@ -7545,6 +7545,8 @@ bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicI
{
QDomEntityPrivate* e = new QDomEntityPrivate(doc, 0, name,
publicId, systemId, notationName);
+ // keep the refcount balanced: appendChild() does a ref anyway.
+ e->ref.deref();
doc->doctype()->appendChild(e);
return true;
}
diff --git a/tests/auto/qdom/tst_qdom.cpp b/tests/auto/qdom/tst_qdom.cpp
index 684e704b30..c4ca8637a4 100644
--- a/tests/auto/qdom/tst_qdom.cpp
+++ b/tests/auto/qdom/tst_qdom.cpp
@@ -133,6 +133,7 @@ private slots:
void taskQTBUG4595_dontAssertWhenDocumentSpecifiesUnknownEncoding() const;
void cloneDTD_QTBUG8398() const;
void DTDNotationDecl();
+ void DTDEntityDecl();
void cleanupTestCase() const;
@@ -1958,5 +1959,29 @@ void tst_QDom::DTDNotationDecl()
QCOMPARE(doctype.namedItem(QString("jpeg")).toNotation().systemId(), QString("image/jpeg"));
}
+void tst_QDom::DTDEntityDecl()
+{
+ QString dtd("<?xml version='1.0' encoding='UTF-8'?>\n"
+ "<!DOCTYPE first [\n"
+ "<!ENTITY secondFile SYSTEM 'second.xml'>\n"
+ "<!ENTITY logo SYSTEM \"http://www.w3c.org/logo.gif\" NDATA gif>"
+ "]>\n"
+ "<first/>\n");
+
+ QDomDocument domDocument;
+ QVERIFY(domDocument.setContent(dtd));
+
+ const QDomDocumentType doctype = domDocument.doctype();
+ QCOMPARE(doctype.entities().count(), 2);
+
+ QVERIFY(doctype.namedItem(QString("secondFile")).isEntity());
+ QCOMPARE(doctype.namedItem(QString("secondFile")).toEntity().systemId(), QString("second.xml"));
+ QCOMPARE(doctype.namedItem(QString("secondFile")).toEntity().notationName(), QString());
+
+ QVERIFY(doctype.namedItem(QString("logo")).isEntity());
+ QCOMPARE(doctype.namedItem(QString("logo")).toEntity().systemId(), QString("http://www.w3c.org/logo.gif"));
+ QCOMPARE(doctype.namedItem(QString("logo")).toEntity().notationName(), QString("gif"));
+}
+
QTEST_MAIN(tst_QDom)
#include "tst_qdom.moc"