Skip to content

fix some memory leaks for parsing & cleaning up rules #2580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions headers/modsecurity/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class Rule {
return *this;
}

virtual ~Rule() {}

virtual bool evaluate(Transaction *transaction) = 0;

virtual bool evaluate(Transaction *transaction,
Expand Down
4 changes: 2 additions & 2 deletions src/parser/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ int Driver::parse(const std::string &f, const std::string &ref) {
m_lastRule = nullptr;
loc.push_back(new yy::location());
if (ref.empty()) {
loc.back()->begin.filename = loc.back()->end.filename = new std::string("<<reference missing or not informed>>");
loc.back()->begin.filename = loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string("<<reference missing or not informed>>"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using std::make_shared to avoid using new

Suggested change
loc.back()->begin.filename = loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string("<<reference missing or not informed>>"));
loc.back()->begin.filename = loc.back()->end.filename = std::make_shared<const std::string>("<<reference missing or not informed>>");

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using std::make_shared to avoid using new

Suggested change
loc.back()->begin.filename = loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string("<<reference missing or not informed>>"));
loc.back()->begin.filename = loc.back()->end.filename = std::make_shared<const std::string>("<<reference missing or not informed>>");

} else {
loc.back()->begin.filename = loc.back()->end.filename = new std::string(ref);
loc.back()->begin.filename = loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(ref));
}

if (f.empty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/location.hh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace yy {
counter_type l = 1,
counter_type c = 1)
{
filename = fn;
filename = std::shared_ptr<filename_type>(fn);
line = l;
column = c;
}
Expand All @@ -105,7 +105,7 @@ namespace yy {
/** \} */

/// File name to which this position refers.
filename_type* filename;
std::shared_ptr<filename_type> filename;
/// Current line number.
counter_type line;
/// Current column number.
Expand Down
2 changes: 1 addition & 1 deletion src/parser/seclang-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ namespace yy {
#line 318 "seclang-parser.yy"
{
// Initialize the initial location.
yyla.location.begin.filename = yyla.location.end.filename = new std::string(driver.file);
yyla.location.begin.filename = yyla.location.end.filename = std::shared_ptr<const std::string>(new std::string(driver.file));
}

#line 1324 "seclang-parser.cc"
Expand Down
2 changes: 1 addition & 1 deletion src/parser/seclang-parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ using namespace modsecurity::operators;
%initial-action
{
// Initialize the initial location.
@$.begin.filename = @$.end.filename = new std::string(driver.file);
@$.begin.filename = @$.end.filename = std::shared_ptr<const std::string>(new std::string(driver.file));
};
%define parse.trace
%define parse.error verbose
Expand Down
6 changes: 3 additions & 3 deletions src/parser/seclang-scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8488,7 +8488,7 @@ YY_RULE_SETUP
std::string err;
std::string f = modsecurity::utils::find_resource(s, *driver.loc.back()->end.filename, &err);
driver.loc.push_back(new yy::location());
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = new std::string(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(f));
yyin = fopen(f.c_str(), "r" );
if (!yyin) {
BEGIN(INITIAL);
Expand Down Expand Up @@ -8519,7 +8519,7 @@ YY_RULE_SETUP
for (auto& s: files) {
std::string f = modsecurity::utils::find_resource(s, *driver.loc.back()->end.filename, &err);
driver.loc.push_back(new yy::location());
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = new std::string(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(f));

yyin = fopen(f.c_str(), "r" );
if (!yyin) {
Expand Down Expand Up @@ -8552,7 +8552,7 @@ YY_RULE_SETUP
c.setKey(key);

driver.loc.push_back(new yy::location());
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = new std::string(url);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(url));
YY_BUFFER_STATE temp = YY_CURRENT_BUFFER;
yypush_buffer_state(temp);

Expand Down
6 changes: 3 additions & 3 deletions src/parser/seclang-scanner.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ EQUALS_MINUS (?i:=\-)
std::string err;
std::string f = modsecurity::utils::find_resource(s, *driver.loc.back()->end.filename, &err);
driver.loc.push_back(new yy::location());
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = new std::string(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(f));
yyin = fopen(f.c_str(), "r" );
if (!yyin) {
BEGIN(INITIAL);
Expand Down Expand Up @@ -1278,7 +1278,7 @@ EQUALS_MINUS (?i:=\-)
for (auto& s: files) {
std::string f = modsecurity::utils::find_resource(s, *driver.loc.back()->end.filename, &err);
driver.loc.push_back(new yy::location());
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = new std::string(f);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(f));

yyin = fopen(f.c_str(), "r" );
if (!yyin) {
Expand Down Expand Up @@ -1307,7 +1307,7 @@ EQUALS_MINUS (?i:=\-)
c.setKey(key);

driver.loc.push_back(new yy::location());
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = new std::string(url);
driver.loc.back()->begin.filename = driver.loc.back()->end.filename = std::shared_ptr<const std::string>(new std::string(url));
YY_BUFFER_STATE temp = YY_CURRENT_BUFFER;
yypush_buffer_state(temp);

Expand Down
4 changes: 4 additions & 0 deletions src/rule_with_actions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ RuleWithActions::RuleWithActions(
m_containsStaticBlockAction(false),
m_isChained(false) {

if (transformations != NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just understand. It's the same like delete actions do.

delete transformations;
}

if (actions) {
for (Action *a : *actions) {
if (a->action_kind == Action::ConfigurationKind) {
Expand Down