From c77ddf6def7a88a3842f2c78e27fe82bf78ce2fc Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 17 Mar 2015 15:08:20 +0000 Subject: [PATCH] Add destruction routine for exprs and atoms. --- src/libutil/expression.c | 33 ++++++++++++++++++++++++++++++++- src/libutil/expression.h | 1 + 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/libutil/expression.c b/src/libutil/expression.c index b5b4dce5f..67db9e7de 100644 --- a/src/libutil/expression.c +++ b/src/libutil/expression.c @@ -251,6 +251,31 @@ rspamd_expr_str_to_op (const gchar *a, const gchar *end, const gchar **next) return op; } +static void +rspamd_expression_destroy (struct rspamd_expression *expr) +{ + guint i; + struct rspamd_expression_elt *elt; + + if (expr != NULL) { + + if (expr->subr->destroy) { + /* Free atoms */ + for (i = 0; i < expr->expressions->len; i ++) { + elt = &g_array_index (expr->expressions, + struct rspamd_expression_elt, i); + + if (elt->type == ELT_ATOM) { + expr->subr->destroy (elt->p.atom); + } + } + } + + g_array_free (expr->expressions, TRUE); + g_array_free (expr->expression_stack, TRUE); + } +} + gboolean rspamd_parse_expression (const gchar *line, gsize len, struct rspamd_atom_subr *subr, gpointer subr_data, @@ -302,7 +327,8 @@ rspamd_parse_expression (const gchar *line, gsize len, /* * First of all, we check some pre-conditions: * 1) if we have 'and ' or 'or ' or 'not ' strings, they are op - * 2) if we have full numeric string, then we check for the following: + * 2) if we have full numeric string, then we check for + * the following expression: * ^\d+\s*[><]$ */ if ((gulong)(end - p) > sizeof ("and ") && @@ -470,6 +496,11 @@ rspamd_parse_expression (const gchar *line, gsize len, if (*target) { *target = e; + rspamd_mempool_add_destructor (pool, + (rspamd_mempool_destruct_t)rspamd_expression_destroy, e); + } + else { + rspamd_expression_destroy (e); } return TRUE; diff --git a/src/libutil/expression.h b/src/libutil/expression.h index 798d3a48c..4179424c6 100644 --- a/src/libutil/expression.h +++ b/src/libutil/expression.h @@ -47,6 +47,7 @@ struct rspamd_atom_subr { gint (*process) (gpointer input, rspamd_expression_atom_t *atom); /* Calculates the relative priority of the expression */ gint (*priority) (rspamd_expression_atom_t *atom); + void (*destroy) (rspamd_expression_atom_t *atom); }; /* Opaque structure */ -- 2.39.5