Ver código fonte

HPCC-21273 Fix memory leaks in ESDL service and xmllib

Signed-off-by: wangkx <kevin.wang@lexisnexis.com>
wangkx 6 anos atrás
pai
commit
08f73248ae

+ 14 - 9
esp/services/esdl_svc_engine/esdl_svc_custom.cpp

@@ -33,7 +33,7 @@ CEsdlCustomTransformChoose::CEsdlCustomTransformChoose(IPropertyTree * choosewhe
         {
             StringBuffer testatt;
             testatt.set(whentree->queryProp("@test"));
-            m_compiledConditionalXpath.set(getCompiledXpath(testatt.str()));
+            m_compiledConditionalXpath.setown(compileXpath(testatt.str()));
 
             compileClauses(whentree, false);
             compileChildChoose(whentree, false);
@@ -136,7 +136,8 @@ void CEsdlCustomTransformChoose::compileChildChoose(IPropertyTree * nested, bool
     ForEach(*conditionalIterator)
     {
         auto xslchooseelement = &conditionalIterator->query();
-        addChildTransformClause(new CEsdlCustomTransformChoose(xslchooseelement), otherwise);
+        Owned<CEsdlCustomTransformChoose> esdlCustomTransformChoose = new CEsdlCustomTransformChoose(xslchooseelement);
+        addChildTransformClause(esdlCustomTransformChoose, otherwise);
     }
 }
 
@@ -170,14 +171,17 @@ void CEsdlCustomTransformChoose::compileClauses(IPropertyTreeIterator * rulesite
                 continue;
         }
 
-        addTransformClause(new CEsdlCustomTransformRule(ruleName, targetFieldPath, pathToValue, optional, typeIsSet ), otherwise);
+        Owned<CEsdlCustomTransformRule> esdlCustomTransformRule = new CEsdlCustomTransformRule(ruleName, targetFieldPath, pathToValue, optional, typeIsSet );
+        addTransformClause(esdlCustomTransformRule, otherwise);
     }
 }
 
 void CEsdlCustomTransformChoose::compileClauses(IPropertyTree * rules, bool otherwise)
 {
-    compileClauses(rules->getElements("xsdl:SetValue"), otherwise, true);
-    compileClauses(rules->getElements("xsdl:AppendValue"), otherwise, false);
+    Owned<IPropertyTreeIterator> setValueItr = rules->getElements("xsdl:SetValue");
+    Owned<IPropertyTreeIterator> appendValueItr = rules->getElements("xsdl:AppendValue");
+    compileClauses(setValueItr, otherwise, true);
+    compileClauses(appendValueItr, otherwise, false);
 }
 
 void CEsdlCustomTransformChoose::addTransformClause(CEsdlCustomTransformRule * fieldmapping, bool otherwise)
@@ -224,10 +228,11 @@ void CEsdlCustomTransformChoose::process(IEspContext * context, IPropertyTree *r
     if (xpath.length())
     {
         //we can use real xpath processing in the future, for now simple substitution is fine
-        xpath.replaceString("{$query}", xpathContext->getVariable("query"));
-        xpath.replaceString("{$method}", xpathContext->getVariable("method"));
-        xpath.replaceString("{$service}", xpathContext->getVariable("service"));
-        xpath.replaceString("{$request}", xpathContext->getVariable("request"));
+        StringBuffer variable;
+        xpath.replaceString("{$query}", xpathContext->getVariable("query", variable));
+        xpath.replaceString("{$method}", xpathContext->getVariable("method", variable.clear()));
+        xpath.replaceString("{$service}", xpathContext->getVariable("service", variable.clear()));
+        xpath.replaceString("{$request}", xpathContext->getVariable("request", variable.clear()));
 
         reqTarget = origTree->queryPropTree(xpath.str());  //get pointer to the write-able area
         if (!reqTarget)

+ 1 - 1
esp/services/esdl_svc_engine/esdl_svc_custom.hpp

@@ -56,7 +56,7 @@ public:
     CEsdlCustomTransformRule(const char * transferName, const char * targetfield, const char * xpathToValue, bool optional, bool attemptToSet)
     :  m_ruleName(transferName), m_targetField(targetfield), m_xpathToValue(xpathToValue), m_optional(optional), m_attemptToSet(attemptToSet)
     {
-        m_compiledValueXpath.set(getCompiledXpath(xpathToValue));
+        m_compiledValueXpath.setown(compileXpath(xpathToValue));
     }
 
 #if defined(_DEBUG)

+ 8 - 3
system/xmllib/libxml_xpathprocessor.cpp

@@ -105,11 +105,16 @@ public:
         return false;
     }
 
-    virtual const char * getVariable(const char * name) override
+    virtual const char * getVariable(const char * name, StringBuffer & variable) override
     {
         ReadLockBlock rblock(m_rwlock);
         if (m_xpathContext)
-            return (const char *)xmlXPathCastToString(xmlXPathVariableLookup(m_xpathContext, (const xmlChar *)name));
+        {
+            xmlXPathObjectPtr ptr = xmlXPathVariableLookup(m_xpathContext, (const xmlChar *)name);
+            variable.append((const char *) ptr->stringval);
+            xmlXPathFreeObject(ptr);
+            return variable;
+        }
         else
             return nullptr;
     }
@@ -291,7 +296,7 @@ private:
     }
 };
 
-extern ICompiledXpath* getCompiledXpath(const char * xpath)
+extern ICompiledXpath* compileXpath(const char * xpath)
 {
     return new CLibCompiledXpath(xpath);
 }

+ 2 - 2
system/xmllib/xpathprocessor.hpp

@@ -33,7 +33,7 @@ interface XMLLIB_API IXpathContext : public IInterface
 public:
 
     virtual bool addVariable(const char * name, const char * val) = 0;
-    virtual const char * getVariable(const char * name) = 0;
+    virtual const char * getVariable(const char * name, StringBuffer & variable) = 0;
     virtual bool evaluateAsBoolean(const char * xpath) = 0;
     virtual bool evaluateAsString(const char * xpath, StringBuffer & evaluated) = 0;
     virtual bool evaluateAsBoolean(ICompiledXpath * compiledXpath) = 0;
@@ -42,7 +42,7 @@ public:
     virtual bool setXmlDoc(const char * xmldoc) = 0;
 };
 
-extern "C" XMLLIB_API ICompiledXpath* getCompiledXpath(const char * xpath);
+extern "C" XMLLIB_API ICompiledXpath* compileXpath(const char * xpath);
 extern "C" XMLLIB_API IXpathContext*  getXpathContext(const char * xmldoc);
 
 #endif /* XPATH_MANAGER_HPP_ */