10000 Fix low-probability memory leak in XMLSERIALIZE(... INDENT). · postgres/postgres@abb517d · GitHub
[go: up one dir, main page]

Skip to content

Commit abb517d

Browse files
committed
Fix low-probability memory leak in XMLSERIALIZE(... INDENT).
xmltotext_with_options() did not consider the possibility that pg_xml_init() could fail --- most likely due to OOM. If that happened, the already-parsed xmlDoc structure would be leaked. Oversight in commit 483bdb2. Bug: #18981 Author: Dmitry Kovalenko <d.kovalenko@postgrespro.ru> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/18981-9bc3c80f107ae925@postgresql.org Backpatch-through: 16
1 parent 3bbc1c4 commit abb517d

File tree

1 file changed

+12
-7
lines changed
  • src/backend/utils/adt

1 file changed

+12
-7
lines changed

src/backend/utils/adt/xml.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
642642
volatile xmlBufferPtr buf = NULL;
643643
volatile xmlSaveCtxtPtr ctxt = NULL;
644644
ErrorSaveContext escontext = {T_ErrorSaveContext};
645-
PgXmlErrorContext *xmlerrcxt;
645+
PgXmlErrorContext *volatile xmlerrcxt = NULL;
646646
#endif
647647

648648
if (xmloption_arg != XMLOPTION_DOCUMENT && !indent)
@@ -683,13 +683,18 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
683683
return (text *) data;
684684
}
685685

686-
/* Otherwise, we gotta spin up some error handling. */
687-
xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
688-
686+
/*
687+
* Otherwise, we gotta spin up some error handling. Unlike most other
688+
* routines in this module, we already have a libxml "doc" structure to
689+
* free, so we need to call pg_xml_init() inside the PG_TRY and be
690+
* prepared for it to fail (typically due to palloc OOM).
691+
*/
689692
PG_TRY();
690693
{
691694
size_t decl_len = 0;
692695

696+
xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
697+
693698
/* The serialized data will go into this buffer. */
694699
buf = xmlBufferCreate();
695700

@@ -817,10 +822,10 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent)
817822
xmlSaveClose(ctxt);
818823
if (buf)
819824
xmlBufferFree(buf);
820-
if (doc)
821-
xmlFreeDoc(doc);
825+
xmlFreeDoc(doc);
822826

823-
pg_xml_done(xmlerrcxt, true);
827+
if (xmlerrcxt)
828+
pg_xml_done(xmlerrcxt, true);
824829

825830
PG_RE_THROW();
826831
}

0 commit comments

Comments
 (0)
0