Skip to content

Commit

Permalink
clearFilter - release memory avoiding crashes
Browse files Browse the repository at this point in the history
clearFilter - delete root Leaf
Leaf constructor: initialize left,right and cond to null pointers
Leaf::clear avoid crashes when called with a null pointer parameter,
and release memory in all cases allocated in the parser
TODO: still crashes if called from DataFilter destructor, see #4249
  • Loading branch information
amtriathlon authored and liversedge committed Oct 16, 2022
1 parent 82e79fa commit 10c2660
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
39 changes: 32 additions & 7 deletions src/Core/DataFilter.cpp
Expand Up @@ -1642,23 +1642,47 @@ bool Leaf::isNumber(DataFilterRuntime *df, Leaf *leaf)

void Leaf::clear(Leaf *leaf)
{
return; // TODO: fix this function to release memory in all Leaf cases avoiding crashes
if (leaf == NULL) return; // critical to avoid crashes

switch(leaf->type) {
case Leaf::Script :
case Leaf::String : delete leaf->lvalue.s; break;
case Leaf::Symbol : delete leaf->lvalue.n; break;
case Leaf::Logical :
case Leaf::BinaryOperation :
case Leaf::Operation : clear(leaf->lvalue.l);
clear(leaf->rvalue.l);
delete(leaf->lvalue.l);
delete(leaf->rvalue.l);
delete leaf->lvalue.l;
delete leaf->rvalue.l;
break;
case Leaf::UnaryOperation : clear(leaf->lvalue.l);
delete leaf->lvalue.l;
break;
case Leaf::Function : clear(leaf->lvalue.l);
delete(leaf->lvalue.l);
break;
default:
break;
delete leaf->lvalue.l;
clear(leaf->series);
delete leaf->series;
foreach (Leaf* l, leaf->fparms) clear(l);
leaf->fparms.clear();
break;
case Leaf::Compound : foreach (Leaf* l, *(leaf->lvalue.b)) clear(l);
delete leaf->lvalue.b;
break;
case Leaf::Conditional : clear(leaf->lvalue.l);
clear(leaf->rvalue.l);
clear(leaf->cond.l);
delete leaf->lvalue.l;
delete leaf->rvalue.l;
delete leaf->cond.l;
break;
case Leaf::Index :
case Leaf::Select : clear(leaf->lvalue.l);
delete leaf->lvalue.l;
foreach (Leaf* l, leaf->fparms) clear(l);
leaf->fparms.clear();
break;
case Leaf::Float :
case Leaf::Integer : break;
}

}
Expand Down Expand Up @@ -3323,6 +3347,7 @@ void DataFilter::clearFilter()
{
if (treeRoot) {
treeRoot->clear(treeRoot);
delete treeRoot;
treeRoot = NULL;
}
rt.isdynamic = false;
Expand Down
3 changes: 2 additions & 1 deletion src/Core/DataFilter.h
Expand Up @@ -114,7 +114,7 @@ class Leaf {

public:

Leaf(int loc, int leng) : type(none),op(0),series(NULL),dynamic(false),loc(loc),leng(leng),inerror(false) { }
Leaf(int loc, int leng) : type(none),lvalue(),rvalue(),cond(),op(0),series(NULL),dynamic(false),loc(loc),leng(leng),inerror(false) { }

// evaluate against a RideItem using its context
//
Expand Down Expand Up @@ -145,6 +145,7 @@ class Leaf {
Compound, Script } type;

union value {
value() { l = NULL; };
float f;
int i;
QString *s;
Expand Down

0 comments on commit 10c2660

Please sign in to comment.