Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf24605e

Browse files
committed
Fix memory leak in XMLSERIALIZE(... INDENT).
xmltotext_with_options sometimes tries to replace the existingroot node of a libxml2 document. In that case xmlDocSetRootElementwill unlink and return the old root node; if we fail to free it,it's leaked for the remainder of the session. The amount of memoryat stake is not large, a couple hundred bytes per occurrence, butthat could still become annoying in heavy usage.Our only other xmlDocSetRootElement call is not at risk becauseit's working on a just-created document, but let's modify thatcode too to make it clear that it's dependent on that.Author: Tom Lane <tgl@sss.pgh.pa.us>Reviewed-by: Jim Jones <jim.jones@uni-muenster.de>Discussion:https://postgr.es/m/1358967.1747858817@sss.pgh.pa.usBackpatch-through: 16
1 parent5d6eac8 commitf24605e

File tree

1 file changed

+17
-4
lines changed
  • src/backend/utils/adt

1 file changed

+17
-4
lines changed

‎src/backend/utils/adt/xml.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,15 +754,22 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
754754
* content nodes, and then iterate over the nodes.
755755
*/
756756
xmlNodePtrroot;
757+
xmlNodePtroldroot;
757758
xmlNodePtrnewline;
758759

759760
root=xmlNewNode(NULL, (constxmlChar*)"content-root");
760761
if (root==NULL||xmlerrcxt->err_occurred)
761762
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
762763
"could not allocate xml node");
763764

764-
/* This attaches root to doc, so we need not free it separately. */
765-
xmlDocSetRootElement(doc,root);
765+
/*
766+
* This attaches root to doc, so we need not free it separately...
767+
* but instead, we have to free the old root if there was one.
768+
*/
769+
oldroot=xmlDocSetRootElement(doc,root);
770+
if (oldroot!=NULL)
771+
xmlFreeNode(oldroot);
772+
766773
xmlAddChildList(root,content_nodes);
767774

768775
/*
@@ -1850,6 +1857,7 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18501857
else
18511858
{
18521859
xmlNodePtrroot;
1860+
xmlNodePtroldrootPG_USED_FOR_ASSERTS_ONLY;
18531861

18541862
/* set up document with empty root node to be the context node */
18551863
doc=xmlNewDoc(version);
@@ -1868,8 +1876,13 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18681876
if (root==NULL||xmlerrcxt->err_occurred)
18691877
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
18701878
"could not allocate xml node");
1871-
/* This attaches root to doc, so we need not free it separately. */
1872-
xmlDocSetRootElement(doc,root);
1879+
1880+
/*
1881+
* This attaches root to doc, so we need not free it separately;
1882+
* and there can't yet be any old root to free.
1883+
*/
1884+
oldroot=xmlDocSetRootElement(doc,root);
1885+
Assert(oldroot==NULL);
18731886

18741887
/* allow empty content */
18751888
if (*(utf8string+count))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp