postgres.git / summary / log / commit / refs

commit    66fb03f53842e58d1025ad04888edc99f04793fa
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Tue Jun 05 00:13:07 2012 +0000
First released in: 9.2.0, 8.3.20, 8.4.13, 9.0.9, 9.1.5

    Fix some more bugs in contrib/xml2's xslt_process().
    
    It failed to check for error return from xsltApplyStylesheet(), as reported
    by Peter Gagarinov.  (So far as I can tell, libxslt provides no convenient
    way to get a useful error message in failure cases.  There might be some
    inconvenient way, but considering that this code is deprecated it's hard to
    get enthusiastic about putting lots of work into it.  So I just made it say
    "failed to apply stylesheet", in line with the existing error checks.)
    
    While looking at the code I also noticed that the string returned by
    xsltSaveResultToString was never freed, resulting in a session-lifespan
    memory leak.
    
    Back-patch to all supported versions.


contrib/xml2/xslt_proc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/contrib/xml2/xslt_proc.c b/contrib/xml2/xslt_proc.c index 830acd42f8c..d6061d08341 100644 --- a/contrib/xml2/xslt_proc.c +++ b/contrib/xml2/xslt_proc.c @@ -116,6 +116,16 @@ xslt_process(PG_FUNCTION_ARGS) } restree = xsltApplyStylesheet(stylesheet, doctree, params); + + if (restree == NULL) + { + xsltFreeStylesheet(stylesheet); + xmlFreeDoc(doctree); + xsltCleanupGlobals(); + xml_ereport(ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION, + "failed to apply stylesheet"); + } + resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet); xsltFreeStylesheet(stylesheet); @@ -124,6 +134,7 @@ xslt_process(PG_FUNCTION_ARGS) xsltCleanupGlobals(); + /* XXX this is pretty dubious, really ought to throw error instead */ if (resstat < 0) PG_RETURN_NULL(); @@ -131,6 +142,9 @@ xslt_process(PG_FUNCTION_ARGS) memcpy(VARDATA(tres), resstr, reslen); SET_VARSIZE(tres, reslen + VARHDRSZ); + if (resstr) + xmlFree(resstr); + PG_RETURN_TEXT_P(tres); #else /* !USE_LIBXSLT */ [parent: 9ced0134a4b0]