fix memory leaks on debashify and minify

This commit is contained in:
zawwz 2021-07-27 22:05:44 +02:00
parent 20e47ab620
commit 3562cb77b2
4 changed files with 36 additions and 21 deletions

View file

@ -103,17 +103,21 @@ bool debashify_bashtest(pipeline* pl)
arg *a=nullptr;
uint32_t j=1;
bool or_op=false;
std::string tmpstr;
for(uint32_t i=1 ; i<in->args->size() ; 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));
cl->add(new pipeline(tbl), or_op);
or_op = a->string() == "||";
or_op = tmpstr == "||";
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];
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 string_subarg("\"") );
affected_args[i].first->add( "\"" );
}
lst->insert(li, *lst_insert );
li+= lst_insert->size();
@ -854,21 +858,25 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params
arg1->add(v->manip->sa[j]);
std::string val=t->val.substr(0, colon_pos);
if(val != "")
arg1->add(new string_subarg(val));
arg1->add(val);
val=t->val.substr(colon_pos+1);
if(val != "")
arg2->add(new string_subarg(val));
arg2->add(val);
for(uint32_t j=i+1; j<v->manip->sa.size(); j++)
arg2->add(v->manip->sa[j]);
delete v->manip->sa[i];
v->manip->sa.resize(0);
break;
// TODO
}
}
}
if(arg1 == nullptr)
{
arg1 = v->manip;
v->manip = nullptr;
}
}
else
{
arg1 = v->manip;
@ -878,13 +886,13 @@ condlist* debashify_manipulation_substring(variable* v, debashify_params* params
pipeline* pl = new pipeline(make_printf_variable(v->varname));
arg* retarg = new arg;
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}));
if(arg2 != nullptr)
{
retarg = new arg;
retarg->add(new string_subarg("-"));
retarg->add("-");
for(auto it: arg2->sa)
{
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}));
}
if(v->manip != nullptr)
delete v->manip;
v->manip = nullptr;
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() == "!")
{
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 string_subarg("}\\\""));
eval_arg->add("}\\\"");
cmd* eval_cmd = make_cmd({new arg("eval"), new arg("echo"), eval_arg});
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")});
arg* sedarg=v->manip;
v->manip = nullptr;
sedarg->insert(0, new string_subarg("s"));
sedarg->add(new string_subarg("/"));
sedarg->insert(0, "s");
sedarg->add("/");
force_quotes(sedarg);
sed->add(sedarg);
// sed "s///g"

View file

@ -480,9 +480,9 @@ bool r_minify_single_block(_obj* in)
// deindex
t->cmds[i]->redirs.resize(0);
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)
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
delete t->cmds[i];

View file

@ -542,7 +542,7 @@ void string_processors(_obj* 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)

View file

@ -113,6 +113,7 @@ arithmetic* make_arithmetic(arg* a)
}; break;
default: break;
}
delete a;
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) )
{
in->sa[i]->quoted=true;
in->insert(i+1, new string_subarg("\""));
in->insert(i, new string_subarg("\""));
in->insert(i+1, "\"");
in->insert(i, "\"");
i+=2;
}
}
@ -151,7 +152,7 @@ void add_quotes(arg* in)
in->sa[i]->quoted=true;
in->insert(0, new string_subarg("\""));
in->add(new string_subarg("\""));
in->add("\"");
}
// ** TESTERS ** //
@ -344,7 +345,7 @@ void condlist::prune_first_cmd()
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]);
t->val += in;
@ -367,15 +368,17 @@ void arg::insert(uint32_t i, subarg* val)
if(val->type == _obj::subarg_string)
{
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]);
t->val += tval->val;
delete val;
}
else if(i<sa.size() && sa[i]->type == _obj::subarg_string)
{
string_subarg* t = dynamic_cast<string_subarg*>(sa[i]);
t->val = tval->val + t->val;
delete val;
}
else
sa.insert(sa.begin()+i, val);