From 711b8dcb02936a6c769d922d0f0e800cb398b8a3 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 16 Oct 2021 12:11:46 +0100 Subject: [PATCH] [Minor] Try to fix cleanup code --- src/libserver/re_cache.c | 63 +++++++++++++++------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/src/libserver/re_cache.c b/src/libserver/re_cache.c index 91ef2cfa4..3016d04c5 100644 --- a/src/libserver/re_cache.c +++ b/src/libserver/re_cache.c @@ -1892,7 +1892,7 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) rspamd_cryptobox_fast_hash_state_t crc_st; guint64 crc; rspamd_regexp_t *re; - hs_compile_error_t *hs_errors; + hs_compile_error_t *hs_errors = NULL; guint *hs_flags = NULL; const hs_expr_ext_t **hs_exts = NULL; gchar **hs_pats = NULL; @@ -2052,8 +2052,24 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) /* Adjust real re number */ n = i; +#define CLEANUP_ALLOCATED(is_err) do { \ + g_free (hs_flags); \ + g_free (hs_ids); \ + for (guint j = 0; j < i; j ++) { \ + g_free (hs_pats[j]); \ + } \ + g_free (hs_pats); \ + g_free (hs_exts); \ + if (is_err) { \ + close (fd); \ + unlink (path); \ + if (hs_errors) hs_free_compile_error (hs_errors); \ + } \ +} while(0) + if (n > 0) { /* Create the hs tree */ + hs_errors = NULL; if (hs_compile_ext_multi ((const char **)hs_pats, hs_flags, hs_ids, @@ -2064,35 +2080,15 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) &test_db, &hs_errors) != HS_SUCCESS) { - - g_free (hs_flags); - g_free (hs_ids); - - for (guint j = 0; j < i; j ++) { - g_free (hs_pats[j]); - } - err = g_error_new (rspamd_re_cache_quark (), EINVAL, "cannot create tree of regexp when processing '%s': %s", hs_pats[hs_errors->expression], hs_errors->message); - g_free (hs_pats); - g_free (hs_exts); - close (fd); - unlink (path); - hs_free_compile_error (hs_errors); - + CLEANUP_ALLOCATED(true); rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); return; } - for (guint j = 0; j < i; j ++) { - g_free (hs_pats[j]); - } - - g_free (hs_pats); - g_free (hs_exts); - if (hs_serialize_database (test_db, &hs_serialized, &serialized_len) != HS_SUCCESS) { err = g_error_new (rspamd_re_cache_quark (), @@ -2100,12 +2096,8 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) "cannot serialize tree of regexp for %s", re_class->hash); - close (fd); - unlink (path); - g_free (hs_ids); - g_free (hs_flags); + CLEANUP_ALLOCATED(true); hs_free_database (test_db); - rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); return; } @@ -2157,10 +2149,8 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) errno, "cannot serialize tree of regexp to %s: %s", path, strerror (errno)); - close (fd); - unlink (path); - g_free (hs_ids); - g_free (hs_flags); + + CLEANUP_ALLOCATED(true); g_free (hs_serialized); rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); @@ -2187,10 +2177,7 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) } cbdata->total += n; - - g_free (hs_serialized); - g_free (hs_ids); - g_free (hs_flags); + CLEANUP_ALLOCATED(false); /* Now rename temporary file to the new .hs file */ rspamd_snprintf (npath, sizeof (npath), "%s%c%s.hs", cbdata->cache_dir, @@ -2219,11 +2206,7 @@ rspamd_re_cache_compile_timer_cb (EV_P_ ev_timer *w, int revents ) (gint)g_hash_table_size (re_class->re), path); - g_free (hs_serialized); - g_free (hs_ids); - g_free (hs_flags); - unlink (path); - close (fd); + CLEANUP_ALLOCATED(true); rspamd_re_cache_compile_err (EV_A_ w, err, cbdata, false); return; -- 2.39.5