Browse Source

Avoid memory leaks in the case of syntax error in the grammatical parser.

test
Michael Uleysky 9 years ago
parent
commit
f779f4075a
  1. 4
      src/globals.cpp
  2. 4
      src/globals.h
  3. 2
      src/main.cpp
  4. 32
      src/parser/grammatical.y

4
src/globals.cpp

@ -7,10 +7,10 @@ std::map<std::string,ObjectBase*> G_vars;
std::multimap<std::string,Func> G_funcs;
// List of objects to save
std::list<ObjectBase*> G_tosave;
std::list<ObjectList*> G_tosave;
// List of objects to print
std::list<ObjectBase*> G_toprint;
std::list<ObjectList*> G_toprint;
void ClearGlobals()
{

4
src/globals.h

@ -13,10 +13,10 @@ typedef ObjectBase* (*Func)(ObjectList*);
extern EXPORT std::multimap<std::string,Func> G_funcs;
// List of objects to save
extern EXPORT std::list<ObjectBase*> EXPORT G_tosave;
extern EXPORT std::list<ObjectList*> EXPORT G_tosave;
// List of objects to print
extern EXPORT std::list<ObjectBase*> EXPORT G_toprint;
extern EXPORT std::list<ObjectList*> EXPORT G_toprint;
void ClearGlobals();

2
src/main.cpp

@ -10,7 +10,7 @@ int main(int argc, char** argv)
SetDebugLevel(INFO);
COUT(INFO)<<"Parse config file "<<argv[1]<<" ";
ret=ParseConfigFile(argv[1]); if(ret!=0) return 1;
ret=ParseConfigFile(argv[1]); if(ret!=0) { ClearGlobals(); return 1;}
COUT(INFO)<<"Ok"<<std::endl;
RegisterArifmeticFunctions();

32
src/parser/grammatical.y

@ -56,6 +56,10 @@ inline void conferror(YYLTYPE *locp, yyscan_t sc, const char * str)
%type <ob> object
%type <ob> pair
%type <ob> list
%destructor {} REAL INTEGER BOOL STRING
%destructor {delete $$;} <*>
%%
input:
%empty {COUT(DEBUG)<<"Empty input\n";}
@ -65,8 +69,32 @@ input:
line:
NAME ASSIGN object ENDL {COUT(DEBUG)<<" NAME ASSIGN object ENDL\n"; if(G_vars.count(*$1)!=0) delete G_vars[*$1]; G_vars[*$1]=$3; delete $1;}
| NAME ASSIGN list ENDL {COUT(DEBUG)<<" NAME ASSIGN list ENDL\n"; if(G_vars.count(*$1)!=0) delete G_vars[*$1]; G_vars[*$1]=$3; delete $1;}
| NAME OBRACE list CBRACE ENDL {COUT(DEBUG)<<" NAME OBRACE list CBRACE ENDL\n"; transform($1->begin(),$1->end(),$1->begin(),::tolower); if(*$1=="save") G_tosave.push_back($3); if(*$1=="print") G_toprint.push_back($3); delete $1;}
| NAME OBRACE object CBRACE ENDL {COUT(DEBUG)<<" NAME OBRACE object CBRACE ENDL\n"; transform($1->begin(),$1->end(),$1->begin(),::tolower); if(*$1=="save") G_tosave.push_back($3); if(*$1=="print") G_toprint.push_back($3); delete $1;}
| NAME OBRACE list CBRACE ENDL {COUT(DEBUG)<<" NAME OBRACE list CBRACE ENDL\n";
transform($1->begin(),$1->end(),$1->begin(),::tolower);
ObjectList* ol=dynamic_cast<ObjectList*>($3);
if(*$1=="save") G_tosave.push_back(ol);
else if(*$1=="print") G_toprint.push_back(ol);
else
{
COUT(ERROR)<<std::endl<<"Unknown statement "<<(*$1)<<std::endl;
delete ol;
delete $1;
YYABORT;
}
delete $1;}
| NAME OBRACE object CBRACE ENDL {COUT(DEBUG)<<" NAME OBRACE object CBRACE ENDL\n";
transform($1->begin(),$1->end(),$1->begin(),::tolower);
ObjectList* ol=new ObjectList($3);
if(*$1=="save") G_tosave.push_back(ol);
else if(*$1=="print") G_toprint.push_back(ol);
else
{
COUT(ERROR)<<std::endl<<"Unknown statement "<<(*$1)<<std::endl;
delete ol;
delete $1;
YYABORT;
}
delete $1;}
;
list:

Loading…
Cancel
Save