]> granicus.if.org Git - postgresql/commitdiff
Fix some more bugs in contrib/xml2's xslt_process().
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 5 Jun 2012 00:12:59 +0000 (20:12 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 5 Jun 2012 00:12:59 +0000 (20:12 -0400)
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

index 4c80732bb8bb5a119a6c8bb75dc881488e8c4337..90232eb0a1ddc808be5fdc7c5a64042667c233d8 100644 (file)
@@ -56,6 +56,7 @@ xslt_process(PG_FUNCTION_ARGS)
 
        text       *doct = PG_GETARG_TEXT_P(0);
        text       *ssheet = PG_GETARG_TEXT_P(1);
+       text       *result;
        text       *paramstr;
        const char *params[MAXPARAMS + 1];      /* +1 for the terminator */
        xsltStylesheetPtr stylesheet = NULL;
@@ -116,6 +117,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,10 +135,16 @@ xslt_process(PG_FUNCTION_ARGS)
 
        xsltCleanupGlobals();
 
+       /* XXX this is pretty dubious, really ought to throw error instead */
        if (resstat < 0)
                PG_RETURN_NULL();
 
-       PG_RETURN_TEXT_P(cstring_to_text_with_len((char *) resstr, reslen));
+       result = cstring_to_text_with_len((char *) resstr, reslen);
+
+       if (resstr)
+               xmlFree(resstr);
+
+       PG_RETURN_TEXT_P(result);
 #else                                                  /* !USE_LIBXSLT */
 
        ereport(ERROR,