Skip to content

Commit 61c1125

Browse files
committed
parser: Fix filename
1 parent 0669c2e commit 61c1125

File tree

7 files changed

+75
-73
lines changed

7 files changed

+75
-73
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.0.4 - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- parser: fix parsed file names
5+
[@zimmerle]
46
- Allow empty anchored variable
57
[Issue #2024 - @airween]
68
- Fixed FILES_NAMES collection after the end of multipart parsing

src/parser/driver.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ int Driver::parse(const std::string &f, const std::string &ref) {
138138
lastRule = NULL;
139139
loc.push_back(new yy::location());
140140
if (ref.empty()) {
141-
this->ref.push_back("<<reference missing or not informed>>");
141+
loc.back()->begin.filename = loc.back()->end.filename = new std::string("<<reference missing or not informed>>");
142142
} else {
143-
this->ref.push_back(ref);
143+
loc.back()->begin.filename = loc.back()->end.filename = new std::string(ref);
144144
}
145145

146146
if (f.empty()) {
@@ -195,9 +195,7 @@ void Driver::error(const yy::location& l, const std::string& m,
195195
const std::string& c) {
196196
if (m_parserError.tellp() == 0) {
197197
m_parserError << "Rules error. ";
198-
if (ref.empty() == false) {
199-
m_parserError << "File: " << ref.back() << ". ";
200-
}
198+
m_parserError << "File: " << *l.end.filename << ". ";
201199
m_parserError << "Line: " << l.end.line << ". ";
202200
m_parserError << "Column: " << l.end.column - 1 << ". ";
203201
}

src/parser/driver.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <list>
2222
#endif
2323

24+
2425
#ifndef SRC_PARSER_DRIVER_H_
2526
#define SRC_PARSER_DRIVER_H_
2627

@@ -50,6 +51,14 @@ typedef struct Driver_t Driver;
5051
#endif
5152

5253

54+
/**
55+
*
56+
* FIXME: There is a memory leak in the filename at yy::location.
57+
* The filename should be converted into a shared string to
58+
* save memory or be associated with the life cycle of the
59+
* driver class.
60+
*
61+
**/
5362
class Driver : public RulesProperties {
5463
public:
5564
Driver();
@@ -77,7 +86,6 @@ class Driver : public RulesProperties {
7786

7887
std::list<yy::location *> loc;
7988

80-
std::list<std::string> ref;
8189
std::string buffer;
8290
Rule *lastRule;
8391
};

src/parser/seclang-parser.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ namespace yy {
11161116
#line 316 "seclang-parser.yy" // lalr1.cc:788
11171117
{
11181118
// Initialize the initial location.
1119-
yyla.location.begin.filename = yyla.location.end.filename = &driver.file;
1119+
yyla.location.begin.filename = yyla.location.end.filename = new std::string(driver.file);
11201120
}
11211121

11221122
#line 1123 "seclang-parser.cc" // lalr1.cc:788
@@ -1707,7 +1707,7 @@ namespace yy {
17071707
{
17081708
yylhs.value.as < std::unique_ptr<Operator> > () = std::move(yystack_[0].value.as < std::unique_ptr<Operator> > ());
17091709
std::string error;
1710-
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(driver.ref.back(), &error) == false) {
1710+
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(*yystack_[0].location.end.filename, &error) == false) {
17111711
driver.error(yystack_[1].location, error);
17121712
YYERROR;
17131713
}
@@ -1721,7 +1721,7 @@ namespace yy {
17211721
yylhs.value.as < std::unique_ptr<Operator> > () = std::move(yystack_[0].value.as < std::unique_ptr<Operator> > ());
17221722
yylhs.value.as < std::unique_ptr<Operator> > ()->m_negation = true;
17231723
std::string error;
1724-
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(driver.ref.back(), &error) == false) {
1724+
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(*yystack_[1].location.end.filename, &error) == false) {
17251725
driver.error(yystack_[2].location, error);
17261726
YYERROR;
17271727
}
@@ -1734,7 +1734,7 @@ namespace yy {
17341734
{
17351735
OPERATOR_CONTAINER(yylhs.value.as < std::unique_ptr<Operator> > (), new operators::Rx(std::move(yystack_[0].value.as < std::unique_ptr<RunTimeString> > ())));
17361736
std::string error;
1737-
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(driver.ref.back(), &error) == false) {
1737+
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(*yystack_[0].location.end.filename, &error) == false) {
17381738
driver.error(yystack_[1].location, error);
17391739
YYERROR;
17401740
}
@@ -1748,7 +1748,7 @@ namespace yy {
17481748
OPERATOR_CONTAINER(yylhs.value.as < std::unique_ptr<Operator> > (), new operators::Rx(std::move(yystack_[0].value.as < std::unique_ptr<RunTimeString> > ())));
17491749
yylhs.value.as < std::unique_ptr<Operator> > ()->m_negation = true;
17501750
std::string error;
1751-
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(driver.ref.back(), &error) == false) {
1751+
if (yylhs.value.as < std::unique_ptr<Operator> > ()->init(*yystack_[1].location.end.filename, &error) == false) {
17521752
driver.error(yystack_[2].location, error);
17531753
YYERROR;
17541754
}
@@ -2063,7 +2063,7 @@ namespace yy {
20632063
/* op */ op,
20642064
/* variables */ v,
20652065
/* actions */ a,
2066-
/* file name */ driver.ref.back(),
2066+
/* file name */ *yystack_[3].location.end.filename,
20672067
/* line number */ yystack_[3].location.end.line
20682068
);
20692069

@@ -2087,7 +2087,7 @@ namespace yy {
20872087
/* op */ yystack_[0].value.as < std::unique_ptr<Operator> > ().release(),
20882088
/* variables */ v,
20892089
/* actions */ NULL,
2090-
/* file name */ driver.ref.back(),
2090+
/* file name */ *yystack_[2].location.end.filename,
20912091
/* line number */ yystack_[2].location.end.line
20922092
);
20932093
if (driver.addSecRule(rule) == false) {
@@ -2109,7 +2109,7 @@ namespace yy {
21092109
/* op */ NULL,
21102110
/* variables */ NULL,
21112111
/* actions */ a,
2112-
/* file name */ driver.ref.back(),
2112+
/* file name */ *yystack_[1].location.end.filename,
21132113
/* line number */ yystack_[1].location.end.line
21142114
);
21152115
driver.addSecAction(rule);
@@ -2128,7 +2128,7 @@ namespace yy {
21282128
RuleScript *r = new RuleScript(
21292129
/* path to script */ yystack_[1].value.as < std::string > (),
21302130
/* actions */ a,
2131-
/* file name */ driver.ref.back(),
2131+
/* file name */ *yystack_[1].location.end.filename,
21322132
/* line number */ yystack_[1].location.end.line
21332133
);
21342134

@@ -2721,7 +2721,7 @@ namespace yy {
27212721
#if defined(WITH_GEOIP) or defined(WITH_MAXMIND)
27222722
std::string err;
27232723
std::string file = modsecurity::utils::find_resource(yystack_[0].value.as < std::string > (),
2724-
driver.ref.back(), &err);
2724+
*yystack_[0].location.end.filename, &err);
27252725
if (file.empty()) {
27262726
std::stringstream ss;
27272727
ss << "Failed to load locate the GeoDB file from: " << yystack_[0].value.as < std::string > () << " ";
@@ -2949,7 +2949,7 @@ namespace yy {
29492949
param.pop_back();
29502950
}
29512951

2952-
file = modsecurity::utils::find_resource(f, driver.ref.back(), &err);
2952+
file = modsecurity::utils::find_resource(f, *yystack_[0].location.end.filename, &err);
29532953
if (file.empty()) {
29542954
std::stringstream ss;
29552955
ss << "Failed to locate the unicode map file from: " << f << " ";

src/parser/seclang-parser.yy

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ using namespace modsecurity::operators;
315315
%initial-action
316316
{
317317
// Initialize the initial location.
318-
@$.begin.filename = @$.end.filename = &driver.file;
318+
@$.begin.filename = @$.end.filename = new std::string(driver.file);
319319
};
320320
%define parse.trace
321321
%define parse.error verbose
@@ -866,7 +866,7 @@ op:
866866
{
867867
$$ = std::move($1);
868868
std::string error;
869-
if ($$->init(driver.ref.back(), &error) == false) {
869+
if ($$->init(*@1.end.filename, &error) == false) {
870870
driver.error(@0, error);
871871
YYERROR;
872872
}
@@ -876,7 +876,7 @@ op:
876876
$$ = std::move($2);
877877
$$->m_negation = true;
878878
std::string error;
879-
if ($$->init(driver.ref.back(), &error) == false) {
879+
if ($$->init(*@1.end.filename, &error) == false) {
880880
driver.error(@0, error);
881881
YYERROR;
882882
}
@@ -885,7 +885,7 @@ op:
885885
{
886886
OPERATOR_CONTAINER($$, new operators::Rx(std::move($1)));
887887
std::string error;
888-
if ($$->init(driver.ref.back(), &error) == false) {
888+
if ($$->init(*@1.end.filename, &error) == false) {
889889
driver.error(@0, error);
890890
YYERROR;
891891
}
@@ -895,7 +895,7 @@ op:
895895
OPERATOR_CONTAINER($$, new operators::Rx(std::move($2)));
896896
$$->m_negation = true;
897897
std::string error;
898-
if ($$->init(driver.ref.back(), &error) == false) {
898+
if ($$->init(*@1.end.filename, &error) == false) {
899899
driver.error(@0, error);
900900
YYERROR;
901901
}
@@ -1073,7 +1073,7 @@ expression:
10731073
/* op */ op,
10741074
/* variables */ v,
10751075
/* actions */ a,
1076-
/* file name */ driver.ref.back(),
1076+
/* file name */ *@1.end.filename,
10771077
/* line number */ @1.end.line
10781078
);
10791079

@@ -1093,7 +1093,7 @@ expression:
10931093
/* op */ $3.release(),
10941094
/* variables */ v,
10951095
/* actions */ NULL,
1096-
/* file name */ driver.ref.back(),
1096+
/* file name */ *@1.end.filename,
10971097
/* line number */ @1.end.line
10981098
);
10991099
if (driver.addSecRule(rule) == false) {
@@ -1111,7 +1111,7 @@ expression:
11111111
/* op */ NULL,
11121112
/* variables */ NULL,
11131113
/* actions */ a,
1114-
/* file name */ driver.ref.back(),
1114+
/* file name */ *@1.end.filename,
11151115
/* line number */ @1.end.line
11161116
);
11171117
driver.addSecAction(rule);
@@ -1126,7 +1126,7 @@ expression:
11261126
RuleScript *r = new RuleScript(
11271127
/* path to script */ $1,
11281128
/* actions */ a,
1129-
/* file name */ driver.ref.back(),
1129+
/* file name */ *@1.end.filename,
11301130
/* line number */ @1.end.line
11311131
);
11321132

@@ -1525,7 +1525,7 @@ expression:
15251525
#if defined(WITH_GEOIP) or defined(WITH_MAXMIND)
15261526
std::string err;
15271527
std::string file = modsecurity::utils::find_resource($1,
1528-
driver.ref.back(), &err);
1528+
*@1.end.filename, &err);
15291529
if (file.empty()) {
15301530
std::stringstream ss;
15311531
ss << "Failed to load locate the GeoDB file from: " << $1 << " ";
@@ -1707,7 +1707,7 @@ expression:
17071707
param.pop_back();
17081708
}
17091709

1710-
file = modsecurity::utils::find_resource(f, driver.ref.back(), &err);
1710+
file = modsecurity::utils::find_resource(f, *@1.end.filename, &err);
17111711
if (file.empty()) {
17121712
std::stringstream ss;
17131713
ss << "Failed to locate the unicode map file from: " << f << " ";

0 commit comments

Comments
 (0)