Skip to content

Commit

Permalink
Gui: fix memory leak in SelectionParser::yyparse()
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Apr 22, 2021
1 parent b4823e8 commit ef4f74a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
26 changes: 26 additions & 0 deletions src/Gui/SelectionFilter.cpp
Expand Up @@ -363,6 +363,31 @@ int fileno(FILE *stream) {return _fileno(stream);}

namespace SelectionParser {

/*!
* \brief The StringFactory class
* Helper class to record the created strings used by the parser.
*/
class StringFactory {
std::list<std::unique_ptr<std::string>> data;
std::size_t max_elements = 20;
public:
static StringFactory* instance() {
static StringFactory* inst = new StringFactory();
return inst;
}
std::string* make(const std::string& str) {
data.push_back(std::make_unique<std::string>(str));
return data.back().get();
}
static std::string* New(const std::string& str) {
return StringFactory::instance()->make(str);
}
void clear() {
if (data.size() > max_elements)
data.clear();
}
};

// show the parser the lexer method
#define yylex SelectionFilterlex
int SelectionFilterlex(void);
Expand Down Expand Up @@ -401,6 +426,7 @@ bool SelectionFilter::parse(void)
Ast.reset(TopBlock);
TopBlock = 0;
SelectionParser::SelectionFilter_delete_buffer (my_string_buffer);
SelectionParser::StringFactory::instance()->clear();

if (Errors.empty()) {
return true;
Expand Down
4 changes: 1 addition & 3 deletions src/Gui/SelectionFilter.h
Expand Up @@ -207,10 +207,8 @@ struct Node_Object
:Slice(slc)
{
ObjectType = Base::Type::fromName(type->c_str());
delete (type);
if(subname){
if (subname) {
SubName = *subname;
delete subname;
}
}
~Node_Object(){
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/SelectionFilter.l
Expand Up @@ -58,7 +58,7 @@
"::" return TNAMESPACE;

[a-zA-Z_][a-zA-Z0-9_]* {
yylval.string = new std::string(yytext);
yylval.string = StringFactory::New(yytext);
return TIDENTIFIER;
}
[0-9]+ {
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/SelectionFilter.tab.c
Expand Up @@ -1345,7 +1345,7 @@ yyparse ()

/* Line 1464 of yacc.c */
#line 40 "SelectionFilter.y"
{ (yyval.string) = new std::string(*(yyvsp[(2) - (4)].string) + "::" + *(yyvsp[(4) - (4)].string)) ;}
{ (yyval.string) = StringFactory::New(*(yyvsp[(2) - (4)].string) + "::" + *(yyvsp[(4) - (4)].string)) ;}
break;

case 4:
Expand Down
22 changes: 11 additions & 11 deletions src/Gui/SelectionFilter.y
Expand Up @@ -55,25 +55,25 @@



type : TSELECT TIDENTIFIER { $$ = $2 }
| TSELECT TIDENTIFIER TNAMESPACE TIDENTIFIER { $$ = new std::string(*$2 + "::" + *$4) }
type : TSELECT TIDENTIFIER { $$ = $2; }
| TSELECT TIDENTIFIER TNAMESPACE TIDENTIFIER { $$ = StringFactory::New(*$2 + "::" + *$4); }

subname : { $$ = 0 }
| TSUB TIDENTIFIER { $$ = $2 }
subname : { $$ = 0; }
| TSUB TIDENTIFIER { $$ = $2; }

count : { $$ = 0 }
| TCOUNT TNUMBER TSLICE TNUMBER { $$ = new Node_Slice($2,$4) }
| TCOUNT TNUMBER TSLICE { $$ = new Node_Slice($2) }
| TCOUNT TNUMBER { $$ = new Node_Slice($2,$2) }
count : { $$ = 0; }
| TCOUNT TNUMBER TSLICE TNUMBER { $$ = new Node_Slice($2,$4); }
| TCOUNT TNUMBER TSLICE { $$ = new Node_Slice($2); }
| TCOUNT TNUMBER { $$ = new Node_Slice($2,$2); }

matchline : type subname count { $$ = new Node_Object($1,$2,$3) }
matchline : type subname count { $$ = new Node_Object($1,$2,$3); }

matchlines : matchline { $$ = new Node_Block($1); }
| matchlines matchline { $$ = $1 ; $$->Objects.emplace_back($2); }

block : matchlines { $$ = $1 }
block : matchlines { $$ = $1; }

filter: block { TopBlock = $1 }
filter: block { TopBlock = $1; }
;


Expand Down
2 changes: 1 addition & 1 deletion src/Gui/lex.SelectionFilter.c
Expand Up @@ -856,7 +856,7 @@ case 8:
YY_RULE_SETUP
#line 41 "SelectionFilter.l"
{
yylval.string = new std::string(SelectionFiltertext);
yylval.string = StringFactory::New(SelectionFiltertext);
return TIDENTIFIER;
}
YY_BREAK
Expand Down

0 comments on commit ef4f74a

Please sign in to comment.