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

Commitee58de1

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 parent0d20635 commitee58de1

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
@@ -733,15 +733,22 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
733733
* content nodes, and then iterate over the nodes.
734734
*/
735735
xmlNodePtrroot;
736+
xmlNodePtroldroot;
736737
xmlNodePtrnewline;
737738

738739
root=xmlNewNode(NULL, (constxmlChar*)"content-root");
739740
if (root==NULL||xmlerrcxt->err_occurred)
740741
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
741742
"could not allocate xml node");
742743

743-
/* This attaches root to doc, so we need not free it separately. */
744-
xmlDocSetRootElement(doc,root);
744+
/*
745+
* This attaches root to doc, so we need not free it separately...
746+
* but instead, we have to free the old root if there was one.
747+
*/
748+
oldroot=xmlDocSetRootElement(doc,root);
749+
if (oldroot!=NULL)
750+
xmlFreeNode(oldroot);
751+
745752
xmlAddChildList(root,content_nodes);
746753

747754
/*
@@ -1829,6 +1836,7 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18291836
else
18301837
{
18311838
xmlNodePtrroot;
1839+
xmlNodePtroldrootPG_USED_FOR_ASSERTS_ONLY;
18321840

18331841
/* set up document with empty root node to be the context node */
18341842
doc=xmlNewDoc(version);
@@ -1847,8 +1855,13 @@ xml_parse(text *data, XmlOptionType xmloption_arg,
18471855
if (root==NULL||xmlerrcxt->err_occurred)
18481856
xml_ereport(xmlerrcxt,ERROR,ERRCODE_OUT_OF_MEMORY,
18491857
"could not allocate xml node");
1850-
/* This attaches root to doc, so we need not free it separately. */
1851-
xmlDocSetRootElement(doc,root);
1858+
1859+
/*
1860+
* This attaches root to doc, so we need not free it separately;
1861+
* and there can't yet be any old root to free.
1862+
*/
1863+
oldroot=xmlDocSetRootElement(doc,root);
1864+
Assert(oldroot==NULL);
18521865

18531866
/* allow empty content */
18541867
if (*(utf8string+count))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp