Skip to content

Commit

Permalink
Merge pull request #406 from MrAnno/fix-literal-opt-leaks
Browse files Browse the repository at this point in the history
Fix memory leaks in literal optimization
  • Loading branch information
alltilla authored Dec 3, 2024
2 parents e22db34 + 98e8f43 commit f67223f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
48 changes: 41 additions & 7 deletions lib/filterx/expr-boolalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ FilterXExpr *
filterx_unary_not_new(FilterXExpr *operand)
{
if (filterx_expr_is_literal(operand))
return filterx_literal_new(filterx_boolean_new(!_literal_expr_truthy(operand)));
{
FilterXExpr *optimized = filterx_literal_new(filterx_boolean_new(!_literal_expr_truthy(operand)));
filterx_expr_unref(operand);
return optimized;
}

FilterXUnaryOp *self = g_new0(FilterXUnaryOp, 1);

Expand Down Expand Up @@ -104,23 +108,38 @@ _eval_and(FilterXExpr *s)
FilterXExpr *
filterx_binary_and_new(FilterXExpr *lhs, FilterXExpr *rhs)
{
FilterXExpr *optimized = NULL;
if (filterx_expr_is_literal(lhs))
{
if (!_literal_expr_truthy(lhs))
return filterx_literal_new(filterx_boolean_new(FALSE));
{
optimized = filterx_literal_new(filterx_boolean_new(FALSE));
goto optimize;
}

if (filterx_expr_is_literal(rhs))
return filterx_literal_new(filterx_boolean_new(_literal_expr_truthy(rhs)));
{
optimized = filterx_literal_new(filterx_boolean_new(_literal_expr_truthy(rhs)));
goto optimize;
}
}

FilterXBinaryOp *self = g_new0(FilterXBinaryOp, 1);

filterx_binary_op_init_instance(self, "and", lhs, rhs);
if (filterx_expr_is_literal(lhs))
self->lhs = NULL;
{
filterx_expr_unref(self->lhs);
self->lhs = NULL;
}

self->super.eval = _eval_and;
return &self->super;

optimize:
filterx_expr_unref(lhs);
filterx_expr_unref(rhs);
return optimized;
}

static FilterXObject *
Expand Down Expand Up @@ -160,21 +179,36 @@ _eval_or(FilterXExpr *s)
FilterXExpr *
filterx_binary_or_new(FilterXExpr *lhs, FilterXExpr *rhs)
{
FilterXExpr *optimized = NULL;
if (filterx_expr_is_literal(lhs))
{
if (_literal_expr_truthy(lhs))
return filterx_literal_new(filterx_boolean_new(TRUE));
{
optimized = filterx_literal_new(filterx_boolean_new(TRUE));
goto optimize;
}

if (filterx_expr_is_literal(rhs))
return filterx_literal_new(filterx_boolean_new(_literal_expr_truthy(rhs)));
{
optimized = filterx_literal_new(filterx_boolean_new(_literal_expr_truthy(rhs)));
goto optimize;
}
}

FilterXBinaryOp *self = g_new0(FilterXBinaryOp, 1);

filterx_binary_op_init_instance(self, "or", lhs, rhs);
if (filterx_expr_is_literal(lhs))
self->lhs = NULL;
{
filterx_expr_unref(self->lhs);
self->lhs = NULL;
}

self->super.eval = _eval_or;
return &self->super;

optimize:
filterx_expr_unref(lhs);
filterx_expr_unref(rhs);
return optimized;
}
2 changes: 2 additions & 0 deletions lib/filterx/expr-null-coalesce.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ filterx_null_coalesce_new(FilterXExpr *lhs, FilterXExpr *rhs)
if (!lhs_object || filterx_object_is_type(lhs_object, &FILTERX_TYPE_NAME(null)))
{
filterx_object_unref(lhs_object);
filterx_expr_unref(lhs);
return rhs;
}

filterx_object_unref(lhs_object);
filterx_expr_unref(rhs);
return lhs;
}

Expand Down

0 comments on commit f67223f

Please sign in to comment.