From 019c4302ad86fd6981e8abc90cd8cb58003b662c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Sun, 8 Dec 2024 22:36:02 +0100 Subject: [PATCH] Handle tiny files gracefully Lua detection functions used to cause fatal errors on failure to read. We could also reconsider treating detection errors as fatal. --- plugins/elf.lua | 2 +- plugins/gzip.lua | 2 +- plugins/pcap.lua | 6 ++++++ plugins/pdf.lua | 2 +- plugins/vdi.lua | 3 +++ plugins/xcursor.lua | 2 +- 6 files changed, 13 insertions(+), 4 deletions(-) diff --git a/plugins/elf.lua b/plugins/elf.lua index 439c112..bf8095f 100644 --- a/plugins/elf.lua +++ b/plugins/elf.lua @@ -18,7 +18,7 @@ -- See man 5 elf, /usr/include/elf.h and /usr/include/llvm/Support/ELF.h local detect = function (c) - return c:read (4) == "\x7FELF" + return #c >= 4 and c:read (4) == "\x7FELF" end local ph_type_table = { diff --git a/plugins/gzip.lua b/plugins/gzip.lua index 4d8bf15..ae5ccb6 100644 --- a/plugins/gzip.lua +++ b/plugins/gzip.lua @@ -16,7 +16,7 @@ -- local detect = function (c) - return c:read (2) == "\x1f\x8b" + return #c >= 2 and c:read (2) == "\x1f\x8b" end local function latin1_to_utf8 (s) diff --git a/plugins/pcap.lua b/plugins/pcap.lua index a02fc8f..7ad6606 100644 --- a/plugins/pcap.lua +++ b/plugins/pcap.lua @@ -16,11 +16,17 @@ -- local detect = function (c) + if #c < 4 then + return false + end local magic = c:u32 () return magic == 0xa1b2c3d4 or magic == 0xd4c3b2a1 end local detect_ng = function (c) + if #c < 8 then + return false + end local magic = c (9):u32 () return c:u32 () == 0x0a0d0d0a and (magic == 0x1a2b3c4d or magic == 0x4d3c2b1a) diff --git a/plugins/pdf.lua b/plugins/pdf.lua index d4c7f26..9703f2d 100644 --- a/plugins/pdf.lua +++ b/plugins/pdf.lua @@ -335,7 +335,7 @@ end -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - local detect = function (c) - return c:read (5) == "%PDF-" + return #c >= 5 and c:read (5) == "%PDF-" end local decode_xref_subsection = function (lex, start, count, result) diff --git a/plugins/vdi.lua b/plugins/vdi.lua index 6124e43..a35c139 100644 --- a/plugins/vdi.lua +++ b/plugins/vdi.lua @@ -16,6 +16,9 @@ -- local detect = function (c) + if #c < 68 then + return false + end c.position = 65 return c:read (4) == "\x7F\x10\xDA\xBE" end diff --git a/plugins/xcursor.lua b/plugins/xcursor.lua index 740736f..3350d34 100644 --- a/plugins/xcursor.lua +++ b/plugins/xcursor.lua @@ -16,7 +16,7 @@ -- local detect = function (c) - return c:read (4) == "Xcur" + return #c >= 4 and c:read (4) == "Xcur" end -- https://www.x.org/releases/current/doc/man/man3/Xcursor.3.xhtml