Skip to content

Commit fb38e38

Browse files
Sami RosendahlQt by Nokia
authored andcommitted
Fix memory leak in QDomDocument DTD notation declaration handler
The created notation 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 DTDNotationDecl to tst_qdom to expose the leak when executed under valgrind memcheck. There was no previous test coverage for the notation declarations in DTD. Task-number: QTBUG-22588 Change-Id: I452d45cb70dfcda48061e0d4af5085ab1c1ad59e (From Qt5 commit 1bc31fa43ddefe468c9f079156bfad0371e2a61b) Reviewed-by: Olivier Goffart <[email protected]>
1 parent 3e4fa9a commit fb38e38

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/xml/dom/qdom.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7557,6 +7557,8 @@ bool QDomHandler::externalEntityDecl(const QString &name, const QString &publicI
75577557
bool QDomHandler::notationDecl(const QString & name, const QString & publicId, const QString & systemId)
75587558
{
75597559
QDomNotationPrivate* n = new QDomNotationPrivate(doc, 0, name, publicId, systemId);
7560+
// keep the refcount balanced: appendChild() does a ref anyway.
7561+
n->ref.deref();
75607562
doc->doctype()->appendChild(n);
75617563
return true;
75627564
}

tests/auto/qdom/tst_qdom.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ private slots:
132132

133133
void taskQTBUG4595_dontAssertWhenDocumentSpecifiesUnknownEncoding() const;
134134
void cloneDTD_QTBUG8398() const;
135+
void DTDNotationDecl();
135136

136137
void cleanupTestCase() const;
137138

@@ -1930,5 +1931,28 @@ void tst_QDom::cloneDTD_QTBUG8398() const
19301931
domDocument2.save(stream, 0);
19311932
QCOMPARE(output, expected);
19321933
}
1934+
1935+
void tst_QDom::DTDNotationDecl()
1936+
{
1937+
QString dtd("<?xml version='1.0' encoding='UTF-8'?>\n"
1938+
"<!DOCTYPE first [\n"
1939+
"<!NOTATION gif SYSTEM 'image/gif'>\n"
1940+
"<!NOTATION jpeg SYSTEM 'image/jpeg'>\n"
1941+
"]>\n"
1942+
"<first/>\n");
1943+
1944+
QDomDocument domDocument;
1945+
QVERIFY(domDocument.setContent(dtd));
1946+
1947+
const QDomDocumentType doctype = domDocument.doctype();
1948+
QCOMPARE(doctype.notations().size(), 2);
1949+
1950+
QVERIFY(doctype.namedItem(QString("gif")).isNotation());
1951+
QCOMPARE(doctype.namedItem(QString("gif")).toNotation().systemId(), QString("image/gif"));
1952+
1953+
QVERIFY(doctype.namedItem(QString("jpeg")).isNotation());
1954+
QCOMPARE(doctype.namedItem(QString("jpeg")).toNotation().systemId(), QString("image/jpeg"));
1955+
}
1956+
19331957
QTEST_MAIN(tst_QDom)
19341958
#include "tst_qdom.moc"

0 commit comments

Comments
 (0)