153aa9179Sopenharmony_ciFrom 85bc313e7996c06d52b6f6f5c6a467ff3a148e75 Mon Sep 17 00:00:00 2001
253aa9179Sopenharmony_ciFrom: Nick Wellnhofer <wellnhofer@aevum.de>
353aa9179Sopenharmony_ciDate: Wed, 15 Feb 2023 13:49:28 +0100
453aa9179Sopenharmony_ciSubject: [PATCH] malloc-fail: Fix memory leak after calling valuePush
553aa9179Sopenharmony_ci
653aa9179Sopenharmony_ciDestroy the object in valuePush if the function fails. This is somewhat
753aa9179Sopenharmony_cidangerous but matches the expectations of users.
853aa9179Sopenharmony_ci
953aa9179Sopenharmony_ciFound with libFuzzer, see #344.
1053aa9179Sopenharmony_ci
1153aa9179Sopenharmony_ciReference:https://github.com/GNOME/libxml2/commit/85bc313e7996c06d52b6f6f5c6a467ff3a148e75
1253aa9179Sopenharmony_ciConflict:NA
1353aa9179Sopenharmony_ci---
1453aa9179Sopenharmony_ci xpath.c | 4 ++++
1553aa9179Sopenharmony_ci 1 file changed, 4 insertions(+)
1653aa9179Sopenharmony_ci
1753aa9179Sopenharmony_cidiff --git a/xpath.c b/xpath.c
1853aa9179Sopenharmony_ciindex 7833870..dc99e63 100644
1953aa9179Sopenharmony_ci--- a/xpath.c
2053aa9179Sopenharmony_ci+++ b/xpath.c
2153aa9179Sopenharmony_ci@@ -2881,6 +2881,8 @@ valuePop(xmlXPathParserContextPtr ctxt)
2253aa9179Sopenharmony_ci  * a memory error is recorded in the parser context.
2353aa9179Sopenharmony_ci  *
2453aa9179Sopenharmony_ci  * Returns the number of items on the value stack, or -1 in case of error.
2553aa9179Sopenharmony_ci+ *
2653aa9179Sopenharmony_ci+ * The object is destroyed in case of error.
2753aa9179Sopenharmony_ci  */
2853aa9179Sopenharmony_ci int
2953aa9179Sopenharmony_ci valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
3053aa9179Sopenharmony_ci@@ -2899,6 +2901,7 @@ valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
3153aa9179Sopenharmony_ci 
3253aa9179Sopenharmony_ci         if (ctxt->valueMax >= XPATH_MAX_STACK_DEPTH) {
3353aa9179Sopenharmony_ci             xmlXPathPErrMemory(ctxt, "XPath stack depth limit reached\n");
3453aa9179Sopenharmony_ci+            xmlXPathFreeObject(value);
3553aa9179Sopenharmony_ci             return (-1);
3653aa9179Sopenharmony_ci         }
3753aa9179Sopenharmony_ci         tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab,
3853aa9179Sopenharmony_ci@@ -2906,6 +2909,7 @@ valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
3953aa9179Sopenharmony_ci                                              sizeof(ctxt->valueTab[0]));
4053aa9179Sopenharmony_ci         if (tmp == NULL) {
4153aa9179Sopenharmony_ci             xmlXPathPErrMemory(ctxt, "pushing value\n");
4253aa9179Sopenharmony_ci+            xmlXPathFreeObject(value);
4353aa9179Sopenharmony_ci             return (-1);
4453aa9179Sopenharmony_ci         }
4553aa9179Sopenharmony_ci         ctxt->valueMax *= 2;
4653aa9179Sopenharmony_ci-- 
4753aa9179Sopenharmony_ci2.27.0
4853aa9179Sopenharmony_ci
49