summaryrefslogtreecommitdiffstats
path: root/lualib/lua_mime.lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2020-10-30 12:04:01 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2020-10-30 12:04:01 +0000
commit01dae7c1959ce6297d3eb47256b43f5811b0e21d (patch)
tree827c23b92d6c6f99e11022c015f3998f9893c247 /lualib/lua_mime.lua
parentaf4de3e677345dcdd7bda3a7a45834581a5970e6 (diff)
downloadrspamd-01dae7c1959ce6297d3eb47256b43f5811b0e21d.tar.gz
rspamd-01dae7c1959ce6297d3eb47256b43f5811b0e21d.zip
[Minor] Lua_mime: Fix several off-by one issues
Diffstat (limited to 'lualib/lua_mime.lua')
-rw-r--r--lualib/lua_mime.lua12
1 files changed, 11 insertions, 1 deletions
diff --git a/lualib/lua_mime.lua b/lualib/lua_mime.lua
index f01f4003a..aab61cb77 100644
--- a/lualib/lua_mime.lua
+++ b/lualib/lua_mime.lua
@@ -338,7 +338,17 @@ local function do_replacement (task, part, mp, replacements,
end
end
end
+ -- Off-by one: match returns 0 based positions while we use 1 based in Lua
+ for _,m in ipairs(matches_flattened) do
+ m[1][1] = m[1][1] - 1
+ m[1][2] = m[1][2] - 1
+ end
+
-- Now flattened match table is sorted by start pos and has the maximum overlapped pattern
+ -- Matches with the same start and end are covering the same replacement
+ -- e.g. we had something like [1 .. 2] -> replacement 1 and [1 .. 4] -> replacement 2
+ -- after flattening we should have [1 .. 4] -> 2 and [1 .. 4] -> 2
+ -- we can safely ignore those duplicates in the following code
local cur_start = 1
local fragments = {}
@@ -349,6 +359,7 @@ local function do_replacement (task, part, mp, replacements,
cur_start = m[1][2] + 1 -- end of match
end
end
+
-- last part
if cur_start < #content then
fragments[#fragments + 1] = content:span(cur_start)
@@ -356,7 +367,6 @@ local function do_replacement (task, part, mp, replacements,
-- Final stuff
out[#out + 1] = {encode_func(rspamd_text.fromtable(fragments)), false}
- out[#out + 1] = {'', true}
else
-- No matches
out[#out + 1] = {part:get_raw_headers(), true}