153aa9179Sopenharmony_ciFrom bd6fa2c1d5c163ab94edaf2e62d18cdfee33f913 Mon Sep 17 00:00:00 2001
253aa9179Sopenharmony_ciFrom: Nick Wellnhofer <wellnhofer@aevum.de>
353aa9179Sopenharmony_ciDate: Thu, 9 Mar 2023 22:33:19 +0100
453aa9179Sopenharmony_ciSubject: [PATCH] malloc-fail: Fix memory leak in xmlXPathRegisterNs
553aa9179Sopenharmony_ci
653aa9179Sopenharmony_ciFound by OSS-Fuzz.
753aa9179Sopenharmony_ci
853aa9179Sopenharmony_ciReference:https://github.com/GNOME/libxml2/commit/bd6fa2c1d5c163ab94edaf2e62d18cdfee33f913
953aa9179Sopenharmony_ciConflict:NA
1053aa9179Sopenharmony_ci---
1153aa9179Sopenharmony_ci xpath.c | 15 +++++++++++++--
1253aa9179Sopenharmony_ci 1 file changed, 13 insertions(+), 2 deletions(-)
1353aa9179Sopenharmony_ci
1453aa9179Sopenharmony_cidiff --git a/xpath.c b/xpath.c
1553aa9179Sopenharmony_ciindex 56a59c1..e3a8c14 100644
1653aa9179Sopenharmony_ci--- a/xpath.c
1753aa9179Sopenharmony_ci+++ b/xpath.c
1853aa9179Sopenharmony_ci@@ -5133,6 +5133,8 @@ xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) {
1953aa9179Sopenharmony_ci int
2053aa9179Sopenharmony_ci xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
2153aa9179Sopenharmony_ci 			   const xmlChar *ns_uri) {
2253aa9179Sopenharmony_ci+    xmlChar *copy;
2353aa9179Sopenharmony_ci+
2453aa9179Sopenharmony_ci     if (ctxt == NULL)
2553aa9179Sopenharmony_ci 	return(-1);
2653aa9179Sopenharmony_ci     if (prefix == NULL)
2753aa9179Sopenharmony_ci@@ -5147,8 +5149,17 @@ xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
2853aa9179Sopenharmony_ci     if (ns_uri == NULL)
2953aa9179Sopenharmony_ci         return(xmlHashRemoveEntry(ctxt->nsHash, prefix,
3053aa9179Sopenharmony_ci 	                          xmlHashDefaultDeallocator));
3153aa9179Sopenharmony_ci-    return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri),
3253aa9179Sopenharmony_ci-			      xmlHashDefaultDeallocator));
3353aa9179Sopenharmony_ci+
3453aa9179Sopenharmony_ci+    copy = xmlStrdup(ns_uri);
3553aa9179Sopenharmony_ci+    if (copy == NULL)
3653aa9179Sopenharmony_ci+        return(-1);
3753aa9179Sopenharmony_ci+    if (xmlHashUpdateEntry(ctxt->nsHash, prefix, copy,
3853aa9179Sopenharmony_ci+                           xmlHashDefaultDeallocator) < 0) {
3953aa9179Sopenharmony_ci+        xmlFree(copy);
4053aa9179Sopenharmony_ci+        return(-1);
4153aa9179Sopenharmony_ci+    }
4253aa9179Sopenharmony_ci+
4353aa9179Sopenharmony_ci+    return(0);
4453aa9179Sopenharmony_ci }
4553aa9179Sopenharmony_ci 
4653aa9179Sopenharmony_ci /**
4753aa9179Sopenharmony_ci-- 
4853aa9179Sopenharmony_ci2.27.0
4953aa9179Sopenharmony_ci
50