From e82e00fb191fa357d20e4cf750d455d112f6292c Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 19 May 2016 13:26:33 +0300 Subject: [PATCH] [Fix] Add sanity checks when compiling regexp maps --- src/libutil/map.c | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/libutil/map.c b/src/libutil/map.c index ae6d59885..1d84e305f 100644 --- a/src/libutil/map.c +++ b/src/libutil/map.c @@ -1955,27 +1955,34 @@ rspamd_re_map_finalize (struct rspamd_regexp_map *re_map) re_map->ids[i] = i; } - if (hs_compile_multi (re_map->patterns, - re_map->flags, - re_map->ids, - re_map->regexps->len, - HS_MODE_BLOCK, - &plt, - &re_map->hs_db, - &err) != HS_SUCCESS) { - - msg_err_map ("cannot create tree of regexp when processing '%s': %s", - re_map->patterns[err->expression], err->message); - re_map->hs_db = NULL; - hs_free_compile_error (err); + if (re_map->regexps->len > 0 && re_map->patterns) { + if (hs_compile_multi (re_map->patterns, + re_map->flags, + re_map->ids, + re_map->regexps->len, + HS_MODE_BLOCK, + &plt, + &re_map->hs_db, + &err) != HS_SUCCESS) { + + msg_err_map ("cannot create tree of regexp when processing '%s': %s", + err->expression >= 0 ? + re_map->patterns[err->expression] : + "unknown regexp", err->message); + re_map->hs_db = NULL; + hs_free_compile_error (err); + + return; + } - return; + if (hs_alloc_scratch (re_map->hs_db, &re_map->hs_scratch) != HS_SUCCESS) { + msg_err_map ("cannot allocate scratch space for hyperscan"); + hs_free_database (re_map->hs_db); + re_map->hs_db = NULL; + } } - - if (hs_alloc_scratch (re_map->hs_db, &re_map->hs_scratch) != HS_SUCCESS) { - msg_err_map ("cannot allocate scratch space for hyperscan"); - hs_free_database (re_map->hs_db); - re_map->hs_db = NULL; + else { + msg_err_map ("regexp map is empty"); } #endif } -- 2.39.5