From 9af2911e8dfa989bcdbf1e3aa2eb999f3f6b7dad Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 18 Sep 2022 20:24:42 +0100 Subject: [PATCH] [Fix] Plug memory leak in regexp destruction with pcre2 --- src/libutil/regexp.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/libutil/regexp.c b/src/libutil/regexp.c index 534187f37..70a50872a 100644 --- a/src/libutil/regexp.c +++ b/src/libutil/regexp.c @@ -116,28 +116,33 @@ rspamd_regexp_dtor (rspamd_regexp_t *re) if (re) { if (re->raw_re && re->raw_re != re->re) { #ifndef WITH_PCRE2 -#ifdef HAVE_PCRE_JIT + /* PCRE1 version */ +# ifdef HAVE_PCRE_JIT if (re->raw_extra) { pcre_free_study (re->raw_extra); } -#endif +# endif #else - if (re->mcontext) { - pcre2_match_context_free (re->mcontext); + /* PCRE 2 version */ + if (re->raw_mcontext) { + pcre2_match_context_free (re->raw_mcontext); } #endif PCRE_FREE (re->raw_re); } + if (re->re) { #ifndef WITH_PCRE2 -#ifdef HAVE_PCRE_JIT + /* PCRE1 version */ +# ifdef HAVE_PCRE_JIT if (re->extra) { pcre_free_study (re->extra); } -#endif +# endif #else - if (re->raw_mcontext) { - pcre2_match_context_free (re->raw_mcontext); + /* PCRE 2 version */ + if (re->mcontext) { + pcre2_match_context_free (re->mcontext); } #endif PCRE_FREE (re->re); @@ -169,15 +174,18 @@ rspamd_regexp_post_process (rspamd_regexp_t *r) pcre2_set_recursion_limit (r->mcontext, max_recursion_depth); pcre2_set_match_limit (r->mcontext, max_backtrack); - if (r->re != r->raw_re) { + if (r->raw_re && r->re != r->raw_re) { r->raw_mcontext = pcre2_match_context_create (NULL); g_assert (r->raw_mcontext != NULL); pcre2_set_recursion_limit (r->raw_mcontext, max_recursion_depth); pcre2_set_match_limit (r->raw_mcontext, max_backtrack); } - else { + else if (r->raw_re) { r->raw_mcontext = r->mcontext; } + else { + r->raw_mcontext = NULL; + } #ifdef HAVE_PCRE_JIT if (can_jit) { -- 2.39.5