From 956534e02ef280795a187c16f6ac04e107f23c5d Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Tue, 4 Aug 2020 19:27:13 +0200 Subject: [PATCH] Check for custom free function in global destructor Calling a custom deallocation function in the global destructor could cause all kinds of unexpected problems. See for example https://github.com/sparklemotion/nokogiri/issues/2059 Only clean up if memory is managed with malloc/free. --- parser.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/parser.c b/parser.c index e1d139d5..c66dbae4 100644 --- a/parser.c +++ b/parser.c @@ -14696,7 +14696,12 @@ xmlCleanupParser(void) { static void ATTRIBUTE_DESTRUCTOR xmlDestructor(void) { - xmlCleanupParser(); + /* + * Calling custom deallocation functions in a destructor can cause + * problems, for example with Nokogiri. + */ + if (xmlFree == free) + xmlCleanupParser(); } #endif