postgres.git / summary / log / commit / refs

commit    7fa0a5f21cdf241a3f19641a6846eabbd2fbcce5
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Mon Nov 02 01:45:36 2026 +0000
First released in: 16.16, 17.12, 18.7, 19.0

    Fix double-free when XMLSERIALIZE(... INDENT) output exceeds 1GB.
    
    In xmltotext_with_options(), xmlSaveClose() frees the save context
    unconditionally, but ctxt was only reset to NULL on the error branch.
    When the close succeeded and a later step threw an error -- typically,
    cstring_to_text_with_len() rejecting a result larger than MaxAllocSize
    -- control reached the PG_CATCH block with ctxt still pointing at the
    freed context, so xmlSaveClose() was called a second time on freed
    memory and crashed the backend.
    
    Author: Jim Jones <jim.jones@uni-muenster.de>
    Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
    Discussion: https://postgr.es/m/bbadb6b2-d226-41a1-ace3-e0eb20648f58@uni-muenster.de
    Backpatch-through: 16


src/backend/utils/adt/xml.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 475d6154911..76b20920de8 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -713,6 +713,7 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) PG_TRY(); { size_t decl_len = 0; + int close_result; xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL); @@ -813,12 +814,12 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) xmlFreeNode(newline); } - if (xmlSaveClose(ctxt) == -1 || xmlerrcxt->err_occurred) - { - ctxt = NULL; /* don't try to close it again */ + close_result = xmlSaveClose(ctxt); + /* xmlSaveClose frees ctxt whether or not it succeeds */ + ctxt = NULL; /* don't try to close it again */ + if (close_result == -1 || xmlerrcxt->err_occurred) xml_ereport(xmlerrcxt, ERROR, ERRCODE_INTERNAL_ERROR, "could not close xmlSaveCtxtPtr"); - } /* * xmlDocContentDumpOutput may add a trailing newline, so remove that. [parent: d6f60a031f21]