Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c168ba3

Browse files
committed
Free libxml2/libxslt resources in a safer order.
Mark Simonetti reported that libxslt sometimes crashes for him, and that swapping xslt_process's object-freeing calls around to do them in reverse order of creation seemed to fix it. I've not reproduced the crash, but valgrind clearly shows a reference to already-freed memory, which is consistent with the idea that shutdown of the xsltTransformContext is trying to reference the already-freed stylesheet or input document. With this patch, valgrind is no longer unhappy. I have an inquiry in to see if this is a libxslt bug or if we're just abusing the library; but even if it's a library bug, we'd want to adjust our code so it doesn't fail with unpatched libraries. Back-patch to all supported branches, because we've been doing this in the wrong(?) order for a long time.
1 parent 143b39c commit c168ba3

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

contrib/xml2/xslt_proc.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,16 @@ xslt_process(PG_FUNCTION_ARGS)
146146
}
147147
PG_CATCH();
148148
{
149-
if (stylesheet != NULL)
150-
xsltFreeStylesheet(stylesheet);
151149
if (restree != NULL)
152150
xmlFreeDoc(restree);
153-
if (doctree != NULL)
154-
xmlFreeDoc(doctree);
155-
if (xslt_sec_prefs != NULL)
156-
xsltFreeSecurityPrefs(xslt_sec_prefs);
157151
if (xslt_ctxt != NULL)
158152
xsltFreeTransformContext(xslt_ctxt);
153+
if (xslt_sec_prefs != NULL)
154+
xsltFreeSecurityPrefs(xslt_sec_prefs);
155+
if (stylesheet != NULL)
156+
xsltFreeStylesheet(stylesheet);
157+
if (doctree != NULL)
158+
xmlFreeDoc(doctree);
159159
xsltCleanupGlobals();
160160

161161
pg_xml_done(xmlerrcxt, true);
@@ -164,11 +164,11 @@ xslt_process(PG_FUNCTION_ARGS)
164164
}
165165
PG_END_TRY();
166166

167-
xsltFreeStylesheet(stylesheet);
168167
xmlFreeDoc(restree);
169-
xmlFreeDoc(doctree);
170-
xsltFreeSecurityPrefs(xslt_sec_prefs);
171168
xsltFreeTransformContext(xslt_ctxt);
169+
xsltFreeSecurityPrefs(xslt_sec_prefs);
170+
xsltFreeStylesheet(stylesheet);
171+
xmlFreeDoc(doctree);
172172
xsltCleanupGlobals();
173173

174174
pg_xml_done(xmlerrcxt, false);

0 commit comments

Comments
 (0)