Skip to content

Commit ceb6b7c

Browse files
Sami Rosendahlthiagomacieira
Sami Rosendahl
authored andcommitted
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 d8d4dc8) Reviewed-by: Thiago Macieira <[email protected]>
1 parent ed2a887 commit ceb6b7c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/xml/dom/qdom.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7545,6 +7545,8 @@ bool QDomHandler::unparsedEntityDecl(const QString &name, const QString &publicI
75457545
{
75467546
QDomEntityPrivate* e = new QDomEntityPrivate(doc, 0, name,
75477547
publicId, systemId, notationName);
7548+
// keep the refcount balanced: appendChild() does a ref anyway.
7549+
e->ref.deref();
75487550
doc->doctype()->appendChild(e);
75497551
return true;
75507552
}

tests/auto/qdom/tst_qdom.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ private slots:
133133
void taskQTBUG4595_dontAssertWhenDocumentSpecifiesUnknownEncoding() const;
134134
void cloneDTD_QTBUG8398() const;
135135
void DTDNotationDecl();
136+
void DTDEntityDecl();
136137

137138
void cleanupTestCase() const;
138139

@@ -1958,5 +1959,29 @@ void tst_QDom::DTDNotationDecl()
19581959
QCOMPARE(doctype.namedItem(QString("jpeg")).toNotation().systemId(), QString("image/jpeg"));
19591960
}
19601961

1962+
void tst_QDom::DTDEntityDecl()
1963+
{
1964+
QString dtd("<?xml version='1.0' encoding='UTF-8'?>\n"
1965+
"<!DOCTYPE first [\n"
1966+
"<!ENTITY secondFile SYSTEM 'second.xml'>\n"
1967+
"<!ENTITY logo SYSTEM \"http://www.w3c.org/logo.gif\" NDATA gif>"
1968+
"]>\n"
1969+
"<first/>\n");
1970+
1971+
QDomDocument domDocument;
1972+
QVERIFY(domDocument.setContent(dtd));
1973+
1974+
const QDomDocumentType doctype = domDocument.doctype();
1975+
QCOMPARE(doctype.entities().count(), 2);
1976+
1977+
QVERIFY(doctype.namedItem(QString("secondFile")).isEntity());
1978+
QCOMPARE(doctype.namedItem(QString("secondFile")).toEntity().systemId(), QString("second.xml"));
1979+
QCOMPARE(doctype.namedItem(QString("secondFile")).toEntity().notationName(), QString());
1980+
1981+
QVERIFY(doctype.namedItem(QString("logo")).isEntity());
1982+
QCOMPARE(doctype.namedItem(QString("logo")).toEntity().systemId(), QString("http://www.w3c.org/logo.gif"));
1983+
QCOMPARE(doctype.namedItem(QString("logo")).toEntity().notationName(), QString("gif"));
1984+
}
1985+
19611986
QTEST_MAIN(tst_QDom)
19621987
#include "tst_qdom.moc"

0 commit comments

Comments
 (0)