From 48704c27a9016bbc9b2e72034782fc13ace2c58a Mon Sep 17 00:00:00 2001 From: Felipe Zimmerle Date: Fri, 30 Oct 2015 17:24:39 -0300 Subject: [PATCH] Removes some memory leaks --- src/assay.cc | 6 ++++++ src/operators/pm.cc | 15 ++++++++++++--- src/operators/pm_from_file.cc | 2 ++ src/parser/seclang-parser.yy | 3 +++ src/rule.cc | 2 ++ src/utils.cc | 5 ++++- src/variables/variations/count.cc | 6 ++++++ test/test-cases/secrules-language-tests | 2 +- 8 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/assay.cc b/src/assay.cc index f7170744..3477e891 100644 --- a/src/assay.cc +++ b/src/assay.cc @@ -683,6 +683,12 @@ int Assay::processRequestBody() { std::string(a->m_key, 16, a->m_key.length() - 16) + ": " \ + a->m_value + "\n"; } + + while (l.empty() == false) { + delete l.front(); + l.pop_front(); + } + fullRequest = fullRequest + "\n\n"; fullRequest = fullRequest + m_requestBody.str(); m_collections.store("FULL_REQUEST", fullRequest); diff --git a/src/operators/pm.cc b/src/operators/pm.cc index b9649e49..fe9eaba8 100644 --- a/src/operators/pm.cc +++ b/src/operators/pm.cc @@ -87,16 +87,18 @@ bool Pm::evaluate(Assay *assay, const std::string &input) { bool Pm::init(const std::string &file, const char **error) { std::vector vec; + std::istringstream *iss; replaceAll(param, "\\", "\\\\"); char *content = parse_pm_content(param.c_str(), param.length(), error); if (content == NULL) { - return false; + iss = new std::istringstream(param); + } else { + iss = new std::istringstream(content); } - std::istringstream iss(param); - std::copy(std::istream_iterator(iss), + std::copy(std::istream_iterator(*iss), std::istream_iterator(), back_inserter(vec)); @@ -106,6 +108,13 @@ bool Pm::init(const std::string &file, const char **error) { acmp_prepare(m_p); + if (content) { + free(content); + content = NULL; + } + + delete iss; + return true; } diff --git a/src/operators/pm_from_file.cc b/src/operators/pm_from_file.cc index 2967007d..cf37901b 100644 --- a/src/operators/pm_from_file.cc +++ b/src/operators/pm_from_file.cc @@ -42,6 +42,7 @@ bool PmFromFile::init(const std::string &config, const char **error) { if (((std::ifstream *)iss)->is_open() == false) { *error = std::string("Failed to open file: " + param).c_str(); + delete iss; return false; } } @@ -52,6 +53,7 @@ bool PmFromFile::init(const std::string &config, const char **error) { acmp_prepare(m_p); + delete iss; return true; } diff --git a/src/parser/seclang-parser.yy b/src/parser/seclang-parser.yy index ccd44f20..3df79d4a 100644 --- a/src/parser/seclang-parser.yy +++ b/src/parser/seclang-parser.yy @@ -420,6 +420,7 @@ expression: if (phase != NULL) { definedPhase = phase->phase; secRuleDefinedPhase = phase->m_secRulesPhase; + delete phase; } else if (a->action_kind == Action::RunTimeOnlyIfMatchKind || a->action_kind == Action::RunTimeBeforeMatchAttemptKind) { None *none = dynamic_cast(a); @@ -449,6 +450,8 @@ expression: for (Action *a : checkedActions) { driver.defaultActions[definedPhase].push_back(a); } + + delete actions; } | CONFIG_DIR_SEC_MARKER { diff --git a/src/rule.cc b/src/rule.cc index 5237c845..5e8927e5 100644 --- a/src/rule.cc +++ b/src/rule.cc @@ -290,6 +290,7 @@ bool Rule::evaluate(Assay *assay) { exclusions.push_back(y->m_key); } exclusions.push_back(variable->name); + delete z; } } @@ -495,6 +496,7 @@ bool Rule::evaluate(Assay *assay) { delete e->front(); e->pop_front(); } + delete e; } return ret; } diff --git a/src/utils.cc b/src/utils.cc index b227f742..1f43e7ec 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -88,7 +88,6 @@ std::vector split(std::string str, char delimiter) { double random_number(const double from, const double to) { std::random_device rd; - return std::bind( std::uniform_real_distribution<>{from, to}, std::default_random_engine{ rd() })(); @@ -1064,16 +1063,20 @@ std::string find_resource(const std::string& resource, iss = new std::ifstream(resource, std::ios::in); if (iss->is_open()) { iss->close(); + delete iss; return resource; } + delete iss; // Trying the same path of the configuration file. std::string f = get_path(config) + "/" + resource; iss = new std::ifstream(f, std::ios::in); if (iss->is_open()) { iss->close(); + delete iss; return f; } + delete iss; return std::string(""); } diff --git a/src/variables/variations/count.cc b/src/variables/variations/count.cc index 988e32ec..3e3c7ab7 100644 --- a/src/variables/variations/count.cc +++ b/src/variables/variations/count.cc @@ -41,6 +41,12 @@ std::list * count++; } + while (reslIn->empty() == false) { + delete reslIn->front(); + reslIn->pop_front(); + } + delete reslIn; + std::string res = std::to_string(count); reslOut->push_back(new transaction::Variable(std::string(var->name), diff --git a/test/test-cases/secrules-language-tests b/test/test-cases/secrules-language-tests index 37cf32eb..e16ceb6e 160000 --- a/test/test-cases/secrules-language-tests +++ b/test/test-cases/secrules-language-tests @@ -1 +1 @@ -Subproject commit 37cf32eb8f939c06923a9ab24dd56a0975c36d4a +Subproject commit e16ceb6ed48dc5af3206220c3c8945def204b9b7