From d3385c8130e4af64e5b2a1648683f6ef6c279849 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 7 Apr 2017 13:03:38 +0100 Subject: [PATCH] [Minor] Fix substring search in case if inlen == srchlen --- src/libutil/str_util.c | 51 +++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index cb110e136..fbf223020 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -1229,36 +1229,41 @@ rspamd_substring_search_caseless (const gchar *in, gsize inlen, gsize i, j; gchar c1, c2; - if (inlen < srchlen) { - return -1; - } + /* Searching */ + if (inlen > srchlen) { + /* Preprocessing */ + for (d = i = 1; i < srchlen; ++i) { + /* computes d = 2^(m-1) with the left-shift operator */ + d = (d << 1); + } - /* Preprocessing */ - for (d = i = 1; i < srchlen; ++i) { - /* computes d = 2^(m-1) with the left-shift operator */ - d = (d << 1); - } + for (hash_in = hash_srch = i = 0; i < srchlen; ++i) { + hash_srch = ((hash_srch << 1) + g_ascii_tolower (srch[i])); + hash_in = ((hash_in << 1) + g_ascii_tolower (in[i])); + } - for (hash_in = hash_srch = i = 0; i < srchlen; ++i) { - hash_srch = ((hash_srch << 1) + g_ascii_tolower (srch[i])); - hash_in = ((hash_in << 1) + g_ascii_tolower (in[i])); - } + j = 0; + while (j <= inlen - srchlen) { - /* Searching */ - j = 0; - while (j <= inlen - srchlen) { + if (hash_srch == hash_in && + rspamd_lc_cmp (srch, in + j, srchlen) == 0) { + return (goffset) j; + } - if (hash_srch == hash_in && rspamd_lc_cmp (srch, in + j, srchlen) == 0) { - return (goffset) j; + c1 = g_ascii_tolower (in[j]); + c2 = g_ascii_tolower (in[j + srchlen]); + hash_in = RKHASH (c1, c2, hash_in); + ++j; } - - c1 = g_ascii_tolower (in[j]); - c2 = g_ascii_tolower (in[j + srchlen]); - hash_in = RKHASH (c1, c2, hash_in); - ++j; + } + else if (inlen == srchlen) { + return rspamd_lc_cmp (srch, in, srchlen) == 0; + } + else { + return (-1); } - return -1; + return (-1); } /* Computing of the maximal suffix for <= */ -- 2.39.5