Fix memory leak of validateDTD's dtd object

This commit is contained in:
Martin Vierula
2023-10-30 15:40:36 -07:00
parent a9edee3dbe
commit beaa452302
3 changed files with 37 additions and 24 deletions

View File

@@ -1,6 +1,6 @@
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
* Copyright (c) 2015 - 2023 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
@@ -33,18 +33,31 @@
namespace modsecurity {
namespace operators {
class XmlDtdPtrManager {
public:
/** @ingroup ModSecurity_Operator */
explicit XmlDtdPtrManager(xmlDtdPtr dtd)
: m_dtd(dtd) { }
~XmlDtdPtrManager() {
#ifdef WITH_LIBXML2
if (m_dtd != NULL) {
xmlFreeDtd(m_dtd);
m_dtd = NULL;
}
#endif
}
xmlDtdPtr get() const {return m_dtd;}
private:
xmlDtdPtr m_dtd; // The resource being managed
};
class ValidateDTD : public Operator {
public:
/** @ingroup ModSecurity_Operator */
explicit ValidateDTD(std::unique_ptr<RunTimeString> param)
: Operator("ValidateDTD", std::move(param)) { }
#ifdef WITH_LIBXML2
~ValidateDTD() {
if (m_dtd != NULL) {
xmlFreeDtd(m_dtd);
m_dtd = NULL;
}
}
~ValidateDTD() { }
bool evaluate(Transaction *transaction, const std::string &str) override;
bool init(const std::string &file, std::string *error) override;
@@ -89,7 +102,6 @@ class ValidateDTD : public Operator {
private:
std::string m_resource;
xmlDtdPtr m_dtd = NULL;
#endif
};