Skip to content

Commit

Permalink
fix: locale parsing related crash on some mpv builds (#794)
Browse files Browse the repository at this point in the history
The # operator produces different results on luajit to the
interpreter for lists like `{1, nil, 3}`.
The interpreter gives a size of 3, while luajit says it's 1.

That caused `table_assign` and `itable_join` to behave differently depending
on the lua environment they're running in.

To get the total number of vararg arguments, `select('#', ...)`` can be
used, which doesn't stop counting when it encounters `nil`.

Any error return nil.
  • Loading branch information
christoph-heinrich committed Nov 13, 2023
1 parent 28e9684 commit 210a121
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/uosc/lib/intl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ function get_locale_from_json(path)

local meta, meta_error = utils.file_info(expand_path)
if not meta or not meta.is_file then
return {}
return nil
end

local json_file = io.open(expand_path, 'r')
if not json_file then
return {}
return nil
end

local json = json_file:read('*all')
json_file:close()

return utils.parse_json(json)
local json_table = utils.parse_json(json)
return json_table
end

---@param text string
Expand Down
6 changes: 3 additions & 3 deletions src/uosc/lib/std.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ end
---@return T[]
function itable_join(...)
local args, result = {...}, {}
for i = 1, #args do
for i = 1, select('#', ...) do
if args[i] then for _, value in ipairs(args[i]) do result[#result + 1] = value end end
end
return result
Expand Down Expand Up @@ -201,8 +201,8 @@ end
---@return T
function table_assign(target, ...)
local args = {...}
for i = 1, #args do
if args[i] then for key, value in pairs(args[i]) do target[key] = value end end
for i = 1, select('#', ...) do
if type(args[i]) == 'table' then for key, value in pairs(args[i]) do target[key] = value end end
end
return target
end
Expand Down

0 comments on commit 210a121

Please sign in to comment.