From b02aae939c1c77d307750c3ce9ad3380d8a93452 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 27 Jan 2022 08:42:54 +0100 Subject: [PATCH] Fix again bug with invalid ignore_files patterns The pattern cannot be tested in advance as it seems that Lua inspect the pattern only partially, the part that is actually used. We resort to use pcall to catch any error when using the pattern. --- data/core/init.lua | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 49a7370c..7e2612aa 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -136,20 +136,23 @@ local function compile_ignore_files() -- config.ignore_files could be a simple string... if type(ipatterns) ~= "table" then ipatterns = {ipatterns} end for i, pattern in ipairs(ipatterns) do - -- we ignore malformed pattern that raise an error - if pcall(string.match, "a", pattern) then - table.insert(compiled, { - use_path = pattern:match("/[^/$]"), -- contains a slash but not at the end - -- An '/' or '/$' at the end means we want to match a directory. - match_dir = pattern:match(".+/%$?$"), -- to be used as a boolen value - pattern = pattern -- get the actual pattern - }) - end + compiled[i] = { + use_path = pattern:match("/[^/$]"), -- contains a slash but not at the end + -- An '/' or '/$' at the end means we want to match a directory. + match_dir = pattern:match(".+/%$?$"), -- to be used as a boolen value + pattern = pattern -- get the actual pattern + } end return compiled end +local function safe_match(s, pattern) + local ok, match = pcall(string.match, s, pattern) + return ok and match +end + + local function fileinfo_pass_filter(info, ignore_compiled) if info.size >= config.file_size_limit * 1e6 then return false end local basename = common.basename(info.filename) @@ -158,11 +161,11 @@ local function fileinfo_pass_filter(info, ignore_compiled) for _, compiled in ipairs(ignore_compiled) do local test = compiled.use_path and fullname or basename if compiled.match_dir then - if info.type == "dir" and string.match(test .. "/", compiled.pattern) then + if info.type == "dir" and safe_match(test .. "/", compiled.pattern) then return false end else - if string.match(test, compiled.pattern) then + if safe_match(test, compiled.pattern) then return false end end