Skip to content

Commit

Permalink
filterx: fix weakref related memory leaks in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed May 8, 2024
1 parent b1b5f00 commit 11b6c6a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 118 deletions.
1 change: 1 addition & 0 deletions lib/filterx/filterx-eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ filterx_eval_store_weak_ref(FilterXObject *object)
{
/* avoid putting object to the list multiple times */
object->weak_referenced = TRUE;
g_assert(context->weak_refs);
g_ptr_array_add(context->weak_refs, filterx_object_ref(object));
}
}
Expand Down
105 changes: 0 additions & 105 deletions lib/filterx/tests/test_filterx_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,13 @@ Test(filterx_expr, test_filterx_literal_evaluates_to_the_literal_object)

Test(filterx_expr, test_filterx_template_evaluates_to_the_expanded_value)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};

filterx_eval_set_context(&context);
FilterXExpr *fexpr = filterx_template_new(compile_template("$HOST $PROGRAM"));
FilterXObject *fobj = filterx_expr_eval(fexpr);

assert_marshaled_object(fobj, "bzorp syslog-ng", LM_VT_STRING);

filterx_expr_unref(fexpr);
log_msg_unref(msg);
filterx_object_unref(fobj);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

struct _FilterXScope
Expand All @@ -107,18 +92,6 @@ struct _FilterXScope

Test(filterx_expr, test_filterx_list_merge)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};
filterx_eval_set_context(&context);

// $fillable = json_array();
FilterXObject *json_array = filterx_json_array_new_empty();
FilterXExpr *fillable = filterx_literal_new(json_array);
Expand Down Expand Up @@ -189,25 +162,10 @@ Test(filterx_expr, test_filterx_list_merge)


filterx_expr_unref(fillable);
log_msg_unref(msg);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

Test(filterx_expr, test_filterx_dict_merge)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};
filterx_eval_set_context(&context);

// $fillable = json();
FilterXObject *json = filterx_json_object_new_empty();
FilterXExpr *fillable = filterx_literal_new(json);
Expand Down Expand Up @@ -342,25 +300,10 @@ Test(filterx_expr, test_filterx_dict_merge)
filterx_object_unref(bar);
filterx_object_unref(foo);
filterx_expr_unref(fillable);
log_msg_unref(msg);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

Test(filterx_expr, test_filterx_assign)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};
filterx_eval_set_context(&context);

FilterXExpr *result_var = filterx_msg_variable_expr_new("$result-var");
cr_assert(result_var != NULL);

Expand All @@ -383,25 +326,10 @@ Test(filterx_expr, test_filterx_assign)
filterx_object_unref(res);
filterx_expr_unref(assign);
filterx_object_unref(result_obj);
log_msg_unref(msg);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

Test(filterx_expr, test_filterx_setattr)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};
filterx_eval_set_context(&context);

FilterXObject *json = filterx_json_object_new_empty();
FilterXExpr *fillable = filterx_literal_new(json);

Expand All @@ -418,25 +346,10 @@ Test(filterx_expr, test_filterx_setattr)
assert_object_json_equals(json, "{\"foo\":\"bar\"}");

filterx_expr_unref(setattr);
log_msg_unref(msg);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

Test(filterx_expr, test_filterx_set_subscript)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};
filterx_eval_set_context(&context);

FilterXObject *json = filterx_json_object_new_empty();
FilterXExpr *fillable = filterx_literal_new(json);

Expand All @@ -455,25 +368,10 @@ Test(filterx_expr, test_filterx_set_subscript)
assert_object_json_equals(json, "{\"foo\":\"bar\"}");

filterx_expr_unref(setattr);
log_msg_unref(msg);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

Test(filterx_expr, test_filterx_readonly)
{
LogMessage *msg = create_sample_message();
FilterXScope *scope = filterx_scope_new();

FilterXEvalContext context =
{
.msgs = &msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = scope,
};
filterx_eval_set_context(&context);

FilterXObject *foo = filterx_string_new("foo", -1);
FilterXObject *bar = filterx_string_new("bar", -1);

Expand Down Expand Up @@ -533,9 +431,6 @@ Test(filterx_expr, test_filterx_readonly)
filterx_expr_unref(literal);
filterx_object_unref(bar);
filterx_object_unref(foo);
log_msg_unref(msg);
filterx_scope_unref(scope);
filterx_eval_set_context(NULL);
}

static void
Expand Down
18 changes: 5 additions & 13 deletions libtest/filterx-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,18 @@ init_libtest_filterx(void)
FILTERX_TYPE_NAME(test_dict) = FILTERX_TYPE_NAME(json_object);
FILTERX_TYPE_NAME(test_list) = FILTERX_TYPE_NAME(json_array);

memset(&filterx_env, 0, sizeof(filterx_env));
filterx_env.msg = create_sample_message();
filterx_env.scope = filterx_scope_new();

filterx_env.context = (FilterXEvalContext)
{
.msgs = &filterx_env.msg,
.num_msg = 1,
.template_eval_options = DEFAULT_TEMPLATE_EVAL_OPTIONS,
.scope = filterx_env.scope,
};
filterx_eval_set_context(&filterx_env.context);
filterx_eval_init_context(&filterx_env.context, NULL);
filterx_env.context.msgs = &filterx_env.msg;
filterx_env.context.num_msg = 1;

}

void
deinit_libtest_filterx(void)
{
log_msg_unref(filterx_env.msg);
filterx_scope_unref(filterx_env.scope);
filterx_eval_set_context(NULL);
filterx_eval_deinit_context(&filterx_env.context);
}

FILTERX_DEFINE_TYPE(test_dict, FILTERX_TYPE_NAME(object));
Expand Down

0 comments on commit 11b6c6a

Please sign in to comment.