Skip to content

Commit ed2a887

Browse files
Sami Rosendahlthiagomacieira
Sami Rosendahl
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: Id211567a5eb9f5552e03756394f994866729dcff (cherry picked from commit fb38e38) Reviewed-by: Thiago Macieira <[email protected]>
1 parent a412194 commit ed2a887

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

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

0 commit comments

Comments
 (0)