153aa9179Sopenharmony_ciFrom d31a0e8e7599bfb691616f7c59ff8d39b982aa55 Mon Sep 17 00:00:00 2001
253aa9179Sopenharmony_ciFrom: Nick Wellnhofer <wellnhofer@aevum.de>
353aa9179Sopenharmony_ciDate: Wed, 15 Feb 2023 14:47:29 +0100
453aa9179Sopenharmony_ciSubject: [PATCH] malloc-fail: Fix memory leak after calling xmlXPathWrapString
553aa9179Sopenharmony_ci
653aa9179Sopenharmony_ciDestroy the string in xmlXPathWrapString if the function fails. This is
753aa9179Sopenharmony_cisomewhat dangerous but matches the expectations of users.
853aa9179Sopenharmony_ci
953aa9179Sopenharmony_ciFound with libFuzzer, see #344.
1053aa9179Sopenharmony_ci
1153aa9179Sopenharmony_ciReference:https://github.com/GNOME/libxml2/commit/d31a0e8e7599bfb691616f7c59ff8d39b982aa55
1253aa9179Sopenharmony_ciConflict:xpath.c
1353aa9179Sopenharmony_ci---
1453aa9179Sopenharmony_ci xpath.c | 3 +++
1553aa9179Sopenharmony_ci 1 file changed, 3 insertions(+)
1653aa9179Sopenharmony_ci
1753aa9179Sopenharmony_cidiff --git a/xpath.c b/xpath.c
1853aa9179Sopenharmony_ciindex 5a6d762..cf74030 100644
1953aa9179Sopenharmony_ci--- a/xpath.c
2053aa9179Sopenharmony_ci+++ b/xpath.c
2153aa9179Sopenharmony_ci@@ -5289,6 +5289,8 @@ xmlXPathNewString(const xmlChar *val) {
2253aa9179Sopenharmony_ci  * Wraps the @val string into an XPath object.
2353aa9179Sopenharmony_ci  *
2453aa9179Sopenharmony_ci  * Returns the newly created object.
2553aa9179Sopenharmony_ci+ *
2653aa9179Sopenharmony_ci+ * Frees @val in case of error.
2753aa9179Sopenharmony_ci  */
2853aa9179Sopenharmony_ci xmlXPathObjectPtr
2953aa9179Sopenharmony_ci xmlXPathWrapString (xmlChar *val) {
3053aa9179Sopenharmony_ci@@ -5297,6 +5299,7 @@ xmlXPathWrapString (xmlChar *val) {
3153aa9179Sopenharmony_ci     ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3253aa9179Sopenharmony_ci     if (ret == NULL) {
3353aa9179Sopenharmony_ci         xmlXPathErrMemory(NULL, "creating string object\n");
3453aa9179Sopenharmony_ci+        xmlFree(val);
3553aa9179Sopenharmony_ci 	return(NULL);
3653aa9179Sopenharmony_ci     }
3753aa9179Sopenharmony_ci     memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3853aa9179Sopenharmony_ci-- 
3953aa9179Sopenharmony_ci2.27.0
4053aa9179Sopenharmony_ci
4153aa9179Sopenharmony_ci
42