fix: crashes when dealing with invalid UTF-8 strings (#579)

Caps char byte count of UTF-8 to string length.

Closes #515
This commit is contained in:
christoph-heinrich
2023-06-28 21:20:36 +02:00
committed by GitHub
parent 442fe6b2e5
commit c8ad77a1a9

View File

@@ -51,11 +51,12 @@ local osd_width, osd_height = 100, 100
---@return integer ---@return integer
local function utf8_char_bytes(str, i) local function utf8_char_bytes(str, i)
local char_byte = str:byte(i) local char_byte = str:byte(i)
if char_byte < 0xC0 then return 1 local max_bytes = #str - i + 1
elseif char_byte < 0xE0 then return 2 if char_byte < 0xC0 then return math.min(max_bytes, 1)
elseif char_byte < 0xF0 then return 3 elseif char_byte < 0xE0 then return math.min(max_bytes, 2)
elseif char_byte < 0xF8 then return 4 elseif char_byte < 0xF0 then return math.min(max_bytes, 3)
else return 1 end elseif char_byte < 0xF8 then return math.min(max_bytes, 4)
else return math.min(max_bytes, 1) end
end end
---Creates an iterator for an utf-8 encoded string ---Creates an iterator for an utf-8 encoded string