Skip to content

Bug: cut utf8 codepoint sequence in half #88

@iacore

Description

@iacore

I don't know how to describe this, but the bug is here:

suit/theme.lua

Lines 136 to 139 in 1767782

if opt.hasKeyboardFocus and (love.timer.getTime() % 1) > .5 then
local ct = input.candidate_text;
local ss = ct.text:sub(1, utf8.offset(ct.text, ct.start))
local ws = opt.font:getWidth(ss)

This bug will cause the application to crash when the first codepoint of candidate text (IME) is not in ASCII range.

Here's a minimal reproducible main.lua:

function hex_dump (str)
    local len = string.len( str )
    local dump = ""
    local hex = ""
    local asc = ""
    
    for i = 1, len do
        if 1 == i % 8 then
            dump = dump .. hex .. asc .. "\n"
            hex = string.format( "%04x: ", i - 1 )
            asc = ""
        end
        
        local ord = string.byte( str, i )
        hex = hex .. string.format( "%02x ", ord )
        if ord >= 32 and ord <= 126 then
            asc = asc .. string.char( ord )
        else
            asc = asc .. "."
        end
    end

    
    return dump .. hex
            .. string.rep( "   ", 8 - len % 8 ) .. asc
end

function fromhex(a)
    local result = ""
    for i,x in ipairs(a) do
        result = result .. string.char(x)
    end
    return result
end

font = love.graphics.getFont( )
utf8 = require "utf8"
local ct = {
    text = fromhex({
        0xe5,0x87,0xb9
    }),
    start = 0,
}
print(ct.text)
local ss = ct.text:sub(1, utf8.offset(ct.text, ct.start))
print("ct.text:")
print(hex_dump(ct.text))
print("ss:")
print(hex_dump(ss))
local ws = font:getWidth(ss) -- crash here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions