Skip to content

Commit

Permalink
fix: crashes when dealing with invalid UTF-8 strings (tomasklaen#579)
Browse files Browse the repository at this point in the history
Caps char byte count of UTF-8 to string length.

Closes tomasklaen#515
  • Loading branch information
christoph-heinrich authored and tam1m committed Oct 2, 2023
1 parent 979d068 commit 7e0ef6e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions scripts/uosc_shared/lib/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7e0ef6e

Please sign in to comment.