From: Vsevolod Stakhov Date: Thu, 14 Jun 2018 12:00:44 +0000 (+0100) Subject: [Fix] Free language detector structures X-Git-Tag: 1.7.6~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=904c6caebe5da551be410d88aa4dd86039f03a4c;p=rspamd.git [Fix] Free language detector structures --- diff --git a/src/libmime/lang_detection.c b/src/libmime/lang_detection.c index 84d23ac63..8763365af 100644 --- a/src/libmime/lang_detection.c +++ b/src/libmime/lang_detection.c @@ -188,6 +188,7 @@ struct rspamd_lang_detector { UConverter *uchar_converter; gsize short_text_limit; gsize total_occurencies; /* number of all languages found */ + ref_entry_t ref; }; static void @@ -622,6 +623,32 @@ rspamd_language_detector_process_chain (struct rspamd_config *cfg, } } +static void +rspamd_language_detector_dtor (struct rspamd_lang_detector *d) +{ + if (d) { + if (d->uchar_converter) { + ucnv_close (d->uchar_converter); + } + + if (d->unicode_scripts) { + g_hash_table_unref (d->unicode_scripts); + } + + if (d->unigramms) { + kh_destroy (rspamd_unigram_hash, d->unigramms); + } + + if (d->trigramms) { + kh_destroy (rspamd_trigram_hash, d->trigramms); + } + + if (d->languages) { + g_ptr_array_free (d->languages, TRUE); + } + } +} + struct rspamd_lang_detector* rspamd_language_detector_init (struct rspamd_config *cfg) { @@ -702,6 +729,12 @@ rspamd_language_detector_init (struct rspamd_config *cfg) (gint)g_hash_table_size (ret->unicode_scripts), (gint)kh_size (ret->unigramms), (gint)kh_size (ret->trigramms)); + + REF_INIT_RETAIN (ret, rspamd_language_detector_dtor); + rspamd_mempool_add_destructor (cfg->cfg_pool, + (rspamd_mempool_destruct_t)rspamd_language_detector_unref, + ret); + end: if (gl.gl_pathc > 0) { globfree (&gl); @@ -1399,3 +1432,18 @@ rspamd_language_detector_detect (struct rspamd_task *task, return result; } + + +struct rspamd_lang_detector* +rspamd_language_detector_ref (struct rspamd_lang_detector* d) +{ + REF_RETAIN (d); + + return d; +} + +void +rspamd_language_detector_unref (struct rspamd_lang_detector* d) +{ + REF_RELEASE (d); +} \ No newline at end of file diff --git a/src/libmime/lang_detection.h b/src/libmime/lang_detection.h index 0058801b8..2d28ec65a 100644 --- a/src/libmime/lang_detection.h +++ b/src/libmime/lang_detection.h @@ -37,6 +37,10 @@ struct rspamd_lang_detector_res { * @return */ struct rspamd_lang_detector* rspamd_language_detector_init (struct rspamd_config *cfg); + +struct rspamd_lang_detector* rspamd_language_detector_ref (struct rspamd_lang_detector* d); +void rspamd_language_detector_unref (struct rspamd_lang_detector* d); + /** * Convert string from utf8 to ucs32 * @param d