From 55e3498a8b639e0983935a2ded98770170a72f96 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 25 Sep 2025 09:31:08 +0100 Subject: [PATCH] [Fix] Fix empty input in lua_magic Issue: #5633 --- lualib/lua_magic/init.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lualib/lua_magic/init.lua b/lualib/lua_magic/init.lua index cef1ddcea8..715358f804 100644 --- a/lualib/lua_magic/init.lua +++ b/lualib/lua_magic/init.lua @@ -306,6 +306,10 @@ exports.detect = function(part, log_obj) if type(input) == 'userdata' then local inplen = #input + if inplen == 0 then + -- Empty input: nothing to match + return nil + end -- Check tail matches if inplen > min_tail_offset then @@ -315,9 +319,11 @@ exports.detect = function(part, log_obj) end -- Try short match - local head = input:span(1, math.min(max_short_offset, inplen)) - match_chunk(head, input, inplen, 0, - compiled_short_patterns, short_patterns, log_obj, res, part) + if max_short_offset > 0 then + local head = input:span(1, math.min(max_short_offset, inplen)) + match_chunk(head, input, inplen, 0, + compiled_short_patterns, short_patterns, log_obj, res, part) + end -- Check if we have enough data or go to long patterns local extensions, confidence = process_detected(res) -- 2.47.3