fix memory leaks on debashify and minify
This commit is contained in:
parent
20e47ab620
commit
3562cb77b2
4 changed files with 36 additions and 21 deletions
|
|
@ -103,17 +103,21 @@ bool debashify_bashtest(pipeline* pl)
|
||||||
arg *a=nullptr;
|
arg *a=nullptr;
|
||||||
uint32_t j=1;
|
uint32_t j=1;
|
||||||
bool or_op=false;
|
bool or_op=false;
|
||||||
|
std::string tmpstr;
|
||||||
for(uint32_t i=1 ; i<in->args->size() ; i++)
|
for(uint32_t i=1 ; i<in->args->size() ; i++)
|
||||||
{
|
{
|
||||||
a = in->args->args[i];
|
a = in->args->args[i];
|
||||||
|
tmpstr = a->string();
|
||||||
|
bool logic_op = ( tmpstr == "&&" || tmpstr == "||" );
|
||||||
|
|
||||||
if(i >= in->args->size()-1 || a->string() == "&&" || a->string() == "||")
|
if(i >= in->args->size()-1 || logic_op)
|
||||||
{
|
{
|
||||||
block* tbl = gen_bashtest_cmd(std::vector<arg*>(in->args->args.begin()+j, in->args->args.begin()+i));
|
block* tbl = gen_bashtest_cmd(std::vector<arg*>(in->args->args.begin()+j, in->args->args.begin()+i));
|
||||||
cl->add(new pipeline(tbl), or_op);
|
cl->add(new pipeline(tbl), or_op);
|
||||||
or_op = a->string() == "||";
|
or_op = tmpstr == "||";
|
||||||
j=i+1;
|
j=i+1;
|
||||||
|
if(logic_op)
|
||||||
|
delete a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -814,7 +818,7 @@ bool debashify_procsub(list* lst, debashify_params* params)
|
||||||
delete affected_args[i].first->sa[0];
|
delete affected_args[i].first->sa[0];
|
||||||
affected_args[i].first->sa[0] = new string_subarg("\"");
|
affected_args[i].first->sa[0] = new string_subarg("\"");
|
||||||
affected_args[i].first->add( new variable_subarg( new variable(strf("_lxshfifo%u", i)) ) );
|
affected_args[i].first->add( new variable_subarg( new variable(strf("_lxshfifo%u", i)) ) );
|
||||||
affected_args[i].first->add( new string_subarg("\"") );
|
affected_args[i].first->add( "\"" );
|
||||||
}
|
}
|
||||||
lst->insert(li, *lst_insert );
|
lst->insert(li, *lst_insert );
|
||||||
li+= lst_insert->size();
|
li+= lst_insert->size();
|
||||||
|
|
@ -854,20 +858,24 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params
|
||||||
arg1->add(v->manip->sa[j]);
|
arg1->add(v->manip->sa[j]);
|
||||||
std::string val=t->val.substr(0, colon_pos);
|
std::string val=t->val.substr(0, colon_pos);
|
||||||
if(val != "")
|
if(val != "")
|
||||||
arg1->add(new string_subarg(val));
|
arg1->add(val);
|
||||||
val=t->val.substr(colon_pos+1);
|
val=t->val.substr(colon_pos+1);
|
||||||
if(val != "")
|
if(val != "")
|
||||||
arg2->add(new string_subarg(val));
|
arg2->add(val);
|
||||||
for(uint32_t j=i+1; j<v->manip->sa.size(); j++)
|
for(uint32_t j=i+1; j<v->manip->sa.size(); j++)
|
||||||
arg2->add(v->manip->sa[j]);
|
arg2->add(v->manip->sa[j]);
|
||||||
|
delete v->manip->sa[i];
|
||||||
|
v->manip->sa.resize(0);
|
||||||
break;
|
break;
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(arg1 == nullptr)
|
if(arg1 == nullptr)
|
||||||
|
{
|
||||||
arg1 = v->manip;
|
arg1 = v->manip;
|
||||||
v->manip = nullptr;
|
v->manip = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -878,13 +886,13 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params
|
||||||
pipeline* pl = new pipeline(make_printf_variable(v->varname));
|
pipeline* pl = new pipeline(make_printf_variable(v->varname));
|
||||||
arg* retarg = new arg;
|
arg* retarg = new arg;
|
||||||
retarg->add(new arithmetic_subarg(make_arithmetic(arg1, "+", new arg("1"))));
|
retarg->add(new arithmetic_subarg(make_arithmetic(arg1, "+", new arg("1"))));
|
||||||
retarg->add(new string_subarg("-"));
|
retarg->add("-");
|
||||||
pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg}));
|
pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg}));
|
||||||
|
|
||||||
if(arg2 != nullptr)
|
if(arg2 != nullptr)
|
||||||
{
|
{
|
||||||
retarg = new arg;
|
retarg = new arg;
|
||||||
retarg->add(new string_subarg("-"));
|
retarg->add("-");
|
||||||
for(auto it: arg2->sa)
|
for(auto it: arg2->sa)
|
||||||
{
|
{
|
||||||
retarg->add(it);
|
retarg->add(it);
|
||||||
|
|
@ -895,6 +903,10 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params
|
||||||
pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg}));
|
pl->add(make_cmd({new arg("cut"), new arg("-c"), retarg}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(v->manip != nullptr)
|
||||||
|
delete v->manip;
|
||||||
|
v->manip = nullptr;
|
||||||
|
|
||||||
return new condlist(pl);
|
return new condlist(pl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -913,9 +925,9 @@ bool debashify_manipulation(arg* in, debashify_params* params)
|
||||||
if(v->is_manip && v->precedence && v->manip->string() == "!")
|
if(v->is_manip && v->precedence && v->manip->string() == "!")
|
||||||
{
|
{
|
||||||
arg* eval_arg = new arg;
|
arg* eval_arg = new arg;
|
||||||
eval_arg->add(new string_subarg("\\\"\\${"));
|
eval_arg->add("\\\"\\${");
|
||||||
eval_arg->add(new variable_subarg(new variable(v->varname)));
|
eval_arg->add(new variable_subarg(new variable(v->varname)));
|
||||||
eval_arg->add(new string_subarg("}\\\""));
|
eval_arg->add("}\\\"");
|
||||||
cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg});
|
cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg});
|
||||||
r = new subshell_subarg(new subshell(eval_cmd));
|
r = new subshell_subarg(new subshell(eval_cmd));
|
||||||
}
|
}
|
||||||
|
|
@ -926,8 +938,8 @@ bool debashify_manipulation(arg* in, debashify_params* params)
|
||||||
cmd* sed = make_cmd({std::string("sed")});
|
cmd* sed = make_cmd({std::string("sed")});
|
||||||
arg* sedarg=v->manip;
|
arg* sedarg=v->manip;
|
||||||
v->manip = nullptr;
|
v->manip = nullptr;
|
||||||
sedarg->insert(0, new string_subarg("s"));
|
sedarg->insert(0, "s");
|
||||||
sedarg->add(new string_subarg("/"));
|
sedarg->add("/");
|
||||||
force_quotes(sedarg);
|
force_quotes(sedarg);
|
||||||
sed->add(sedarg);
|
sed->add(sedarg);
|
||||||
// sed "s///g"
|
// sed "s///g"
|
||||||
|
|
|
||||||
|
|
@ -480,9 +480,9 @@ bool r_minify_single_block(_obj* in)
|
||||||
// deindex
|
// deindex
|
||||||
t->cmds[i]->redirs.resize(0);
|
t->cmds[i]->redirs.resize(0);
|
||||||
if(t->cmds[i]->type == _obj::block_brace)
|
if(t->cmds[i]->type == _obj::block_brace)
|
||||||
dynamic_cast<brace*>(t->cmds[i])->lst->cls[0]->pls[0] = nullptr;
|
dynamic_cast<brace*>(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr;
|
||||||
else if(t->cmds[i]->type == _obj::block_subshell)
|
else if(t->cmds[i]->type == _obj::block_subshell)
|
||||||
dynamic_cast<subshell*>(t->cmds[i])->lst->cls[0]->pls[0] = nullptr;
|
dynamic_cast<subshell*>(t->cmds[i])->lst->cls[0]->pls[0]->cmds[0] = nullptr;
|
||||||
|
|
||||||
// replace value
|
// replace value
|
||||||
delete t->cmds[i];
|
delete t->cmds[i];
|
||||||
|
|
|
||||||
|
|
@ -542,7 +542,7 @@ void string_processors(_obj* in)
|
||||||
|
|
||||||
std::string quote_string(std::string const& in)
|
std::string quote_string(std::string const& in)
|
||||||
{
|
{
|
||||||
return '"' + stringReplace(in, "\"", "\\\"") + '"';
|
return '"' + stringReplace(stringReplace(stringReplace(in, "\\", "\\\\"), "\"", "\\\""), "\n", "\\n") + '"';
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string gen_json(std::vector<std::pair<std::string,std::string>> const& vec)
|
std::string gen_json(std::vector<std::pair<std::string,std::string>> const& vec)
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,7 @@ arithmetic* make_arithmetic(arg* a)
|
||||||
}; break;
|
}; break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
delete a;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,8 +139,8 @@ void force_quotes(arg* in)
|
||||||
if(!in->sa[i]->quoted && (in->sa[i]->type == _obj::subarg_variable || in->sa[i]->type == _obj::subarg_subshell) )
|
if(!in->sa[i]->quoted && (in->sa[i]->type == _obj::subarg_variable || in->sa[i]->type == _obj::subarg_subshell) )
|
||||||
{
|
{
|
||||||
in->sa[i]->quoted=true;
|
in->sa[i]->quoted=true;
|
||||||
in->insert(i+1, new string_subarg("\""));
|
in->insert(i+1, "\"");
|
||||||
in->insert(i, new string_subarg("\""));
|
in->insert(i, "\"");
|
||||||
i+=2;
|
i+=2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +152,7 @@ void add_quotes(arg* in)
|
||||||
in->sa[i]->quoted=true;
|
in->sa[i]->quoted=true;
|
||||||
|
|
||||||
in->insert(0, new string_subarg("\""));
|
in->insert(0, new string_subarg("\""));
|
||||||
in->add(new string_subarg("\""));
|
in->add("\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ** TESTERS ** //
|
// ** TESTERS ** //
|
||||||
|
|
@ -344,7 +345,7 @@ void condlist::prune_first_cmd()
|
||||||
|
|
||||||
void arg::insert(uint32_t i, std::string const& in)
|
void arg::insert(uint32_t i, std::string const& in)
|
||||||
{
|
{
|
||||||
if(i>0 && sa[i-1]->type == _obj::subarg_string)
|
if(i>0 && i<=sa.size() && sa[i-1]->type == _obj::subarg_string)
|
||||||
{
|
{
|
||||||
string_subarg* t = dynamic_cast<string_subarg*>(sa[i-1]);
|
string_subarg* t = dynamic_cast<string_subarg*>(sa[i-1]);
|
||||||
t->val += in;
|
t->val += in;
|
||||||
|
|
@ -367,15 +368,17 @@ void arg::insert(uint32_t i, subarg* val)
|
||||||
if(val->type == _obj::subarg_string)
|
if(val->type == _obj::subarg_string)
|
||||||
{
|
{
|
||||||
string_subarg* tval = dynamic_cast<string_subarg*>(val);
|
string_subarg* tval = dynamic_cast<string_subarg*>(val);
|
||||||
if(i>0 && sa[i-1]->type == _obj::subarg_string)
|
if(i>0 && i<=sa.size() && sa[i-1]->type == _obj::subarg_string)
|
||||||
{
|
{
|
||||||
string_subarg* t = dynamic_cast<string_subarg*>(sa[i-1]);
|
string_subarg* t = dynamic_cast<string_subarg*>(sa[i-1]);
|
||||||
t->val += tval->val;
|
t->val += tval->val;
|
||||||
|
delete val;
|
||||||
}
|
}
|
||||||
else if(i<sa.size() && sa[i]->type == _obj::subarg_string)
|
else if(i<sa.size() && sa[i]->type == _obj::subarg_string)
|
||||||
{
|
{
|
||||||
string_subarg* t = dynamic_cast<string_subarg*>(sa[i]);
|
string_subarg* t = dynamic_cast<string_subarg*>(sa[i]);
|
||||||
t->val = tval->val + t->val;
|
t->val = tval->val + t->val;
|
||||||
|
delete val;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
sa.insert(sa.begin()+i, val);
|
sa.insert(sa.begin()+i, val);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue