From f8b4248e1f56fa0b9f4cd850f46c7ff544674b15 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 8 Dec 2020 13:53:29 +0000 Subject: [PATCH] [Minor] Improve composites processing strncmp -> memcmp --- src/libserver/composites.c | 140 ++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 63 deletions(-) diff --git a/src/libserver/composites.c b/src/libserver/composites.c index 3ec770439..7a5289441 100644 --- a/src/libserver/composites.c +++ b/src/libserver/composites.c @@ -611,51 +611,23 @@ rspamd_composite_expr_process (void *ud, } sym = comp_atom->symbol; + guint slen = strlen (sym); while (*sym != '\0' && !g_ascii_isalnum (*sym)) { sym ++; + slen --; } - if (strncmp (sym, "g:", 2) == 0) { - gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 2); + if (slen > 2) { + if (G_UNLIKELY (memcmp (sym, "g:", 2) == 0)) { + gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 2); - if (gr != NULL) { - g_hash_table_iter_init (&it, gr->symbols); + if (gr != NULL) { + g_hash_table_iter_init (&it, gr->symbols); - while (g_hash_table_iter_next (&it, &k, &v)) { - sdef = v; - rc = rspamd_composite_process_single_symbol (cd, sdef->name, &ms, - comp_atom); - - if (rc) { - rspamd_composite_process_symbol_removal (atom, - cd, - ms, - comp_atom->symbol); - - if (fabs (rc) > max) { - max = fabs (rc); - } - } - } - } - - rc = max; - } - else if (strncmp (sym, "g+:", 3) == 0) { - /* Group, positive symbols only */ - gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 3); - - if (gr != NULL) { - g_hash_table_iter_init (&it, gr->symbols); - - while (g_hash_table_iter_next (&it, &k, &v)) { - sdef = v; - - if (sdef->score > 0) { - rc = rspamd_composite_process_single_symbol (cd, - sdef->name, - &ms, + while (g_hash_table_iter_next (&it, &k, &v)) { + sdef = v; + rc = rspamd_composite_process_single_symbol (cd, sdef->name, &ms, comp_atom); if (rc) { @@ -673,37 +645,79 @@ rspamd_composite_expr_process (void *ud, rc = max; } - } - else if (strncmp (sym, "g-:", 3) == 0) { - /* Group, negative symbols only */ - gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 3); - - if (gr != NULL) { - g_hash_table_iter_init (&it, gr->symbols); - - while (g_hash_table_iter_next (&it, &k, &v)) { - sdef = v; - - if (sdef->score < 0) { - rc = rspamd_composite_process_single_symbol (cd, - sdef->name, - &ms, - comp_atom); - - if (rc) { - rspamd_composite_process_symbol_removal (atom, - cd, - ms, - comp_atom->symbol); + else if (G_UNLIKELY (memcmp (sym, "g+:", 3) == 0)) { + /* Group, positive symbols only */ + gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 3); + + if (gr != NULL) { + g_hash_table_iter_init (&it, gr->symbols); + + while (g_hash_table_iter_next (&it, &k, &v)) { + sdef = v; + + if (sdef->score > 0) { + rc = rspamd_composite_process_single_symbol (cd, + sdef->name, + &ms, + comp_atom); + + if (rc) { + rspamd_composite_process_symbol_removal (atom, + cd, + ms, + comp_atom->symbol); + + if (fabs (rc) > max) { + max = fabs (rc); + } + } + } + } - if (fabs (rc) > max) { - max = fabs (rc); + rc = max; + } + } + else if (G_UNLIKELY (memcmp (sym, "g-:", 3) == 0)) { + /* Group, negative symbols only */ + gr = g_hash_table_lookup (cd->task->cfg->groups, sym + 3); + + if (gr != NULL) { + g_hash_table_iter_init (&it, gr->symbols); + + while (g_hash_table_iter_next (&it, &k, &v)) { + sdef = v; + + if (sdef->score < 0) { + rc = rspamd_composite_process_single_symbol (cd, + sdef->name, + &ms, + comp_atom); + + if (rc) { + rspamd_composite_process_symbol_removal (atom, + cd, + ms, + comp_atom->symbol); + + if (fabs (rc) > max) { + max = fabs (rc); + } } } } + + rc = max; } + } + else { + rc = rspamd_composite_process_single_symbol (cd, sym, &ms, comp_atom); - rc = max; + if (rc) { + rspamd_composite_process_symbol_removal (atom, + cd, + ms, + comp_atom->symbol); + } } } else { -- 2.39.5