8000 Fix some more bugs in contrib/xml2's xslt_process(). · sureandrew/postgres@66fb03f · GitHub
[go: up one dir, main page]

Skip to content

Commit 66fb03f

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 9ced013 commit 66fb03f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

contrib/xml2/xslt_proc.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ xslt_process(PG_FUNCTION_ARGS)
116116
}
117117

118118
restree = xsltApplyStylesheet(stylesheet, doctree, params);
119+
120+
if (restree == NULL)
121+
{
122+
xsltFreeStylesheet(stylesheet);
123+
xmlFreeDoc(doctree);
124+
xsltCleanupGlobals();
125+
xml_ereport(ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
126+
"failed to apply stylesheet");
127+
}
128+
119129
resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
120130

121131
xsltFreeStylesheet(stylesheet);
@@ -124,13 +134,17 @@ xslt_process(PG_FUNCTION_ARGS)
124134

125135
xsltCleanupGlobals();
126136

137+
/* XXX this is pretty dubious, really ought to throw error instead */
127138
if (resstat < 0)
128139
PG_RETURN_NULL();
129140

130141
tres = palloc(reslen + VARHDRSZ);
131142
memcpy(VARDATA(tres), resstr, reslen);
132143
SET_VARSIZE(tres, reslen + VARHDRSZ);
133144

145+
if (resstr)
146+
xmlFree(resstr);
147+
134148
PG_RETURN_TEXT_P(tres);
135149

136150
#else /* !USE_LIBXSLT */

0 commit comments

Comments
 (0)
0