From c8ad77a1a92d0667e1e66f11e84692cd03796ec8 Mon Sep 17 00:00:00 2001 From: christoph-heinrich Date: Wed, 28 Jun 2023 21:20:36 +0200 Subject: [PATCH] fix: crashes when dealing with invalid UTF-8 strings (#579) Caps char byte count of UTF-8 to string length. Closes #515 --- scripts/uosc_shared/lib/text.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/scripts/uosc_shared/lib/text.lua b/scripts/uosc_shared/lib/text.lua index d573b81..0af8a6c 100644 --- a/scripts/uosc_shared/lib/text.lua +++ b/scripts/uosc_shared/lib/text.lua @@ -51,11 +51,12 @@ local osd_width, osd_height = 100, 100 ---@return integer local function utf8_char_bytes(str, i) local char_byte = str:byte(i) - if char_byte < 0xC0 then return 1 - elseif char_byte < 0xE0 then return 2 - elseif char_byte < 0xF0 then return 3 - elseif char_byte < 0xF8 then return 4 - else return 1 end + local max_bytes = #str - i + 1 + if char_byte < 0xC0 then return math.min(max_bytes, 1) + elseif char_byte < 0xE0 then return math.min(max_bytes, 2) + elseif char_byte < 0xF0 then return math.min(max_bytes, 3) + elseif char_byte < 0xF8 then return math.min(max_bytes, 4) + else return math.min(max_bytes, 1) end end ---Creates an iterator for an utf-8 encoded string