Skip to content

Commit d9d7214

Browse files
committed
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.
1 parent 873d1c1 commit d9d7214

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

contrib/xml2/xslt_proc.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ xslt_process(PG_FUNCTION_ARGS)
5454

5555
text *doct = PG_GETARG_TEXT_P(0);
5656
text *ssheet = PG_GETARG_TEXT_P(1);
57+
text *result;
5758
text *paramstr;
5859
const char **params;
5960
xsltStylesheetPtr stylesheet = NULL;
@@ -117,6 +118,16 @@ xslt_process(PG_FUNCTION_ARGS)
117118
}
118119

119120
restree = xsltApplyStylesheet(stylesheet, doctree, params);
121+
122+
if (restree == NULL)
123+
{
124+
xsltFreeStylesheet(stylesheet);
125+
xmlFreeDoc(doctree);
126+
xsltCleanupGlobals();
127+
xml_ereport(ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
128+
"failed to apply stylesheet");
129+
}
130+
120131
resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
121132

122133
xsltFreeStylesheet(stylesheet);
@@ -125,10 +136,16 @@ xslt_process(PG_FUNCTION_ARGS)
125136

126137
xsltCleanupGlobals();
127138

139+
/* XXX this is pretty dubious, really ought to throw error instead */
128140
if (resstat < 0)
129141
PG_RETURN_NULL();
130142

131-
PG_RETURN_TEXT_P(cstring_to_text_with_len((char *) resstr, reslen));
143+
result = cstring_to_text_with_len((char *) resstr, reslen);
144+
145+
if (resstr)
146+
xmlFree(resstr);
147+
148+
PG_RETURN_TEXT_P(result);
132149
#else /* !USE_LIBXSLT */
133150

134151
ereport(ERROR,

0 commit comments

Comments
 (0)