diff --git a/lib/filterx/expr-boolalg.c b/lib/filterx/expr-boolalg.c index f4a9760a2..b10dd5aad 100644 --- a/lib/filterx/expr-boolalg.c +++ b/lib/filterx/expr-boolalg.c @@ -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); @@ -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 * @@ -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; } diff --git a/lib/filterx/expr-null-coalesce.c b/lib/filterx/expr-null-coalesce.c index 019ef8b14..dd25d15f8 100644 --- a/lib/filterx/expr-null-coalesce.c +++ b/lib/filterx/expr-null-coalesce.c @@ -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; }