From 210a1216feed0eeb7e780b93971fa562094f6b91 Mon Sep 17 00:00:00 2001 From: christoph-heinrich Date: Mon, 13 Nov 2023 18:58:43 +0100 Subject: [PATCH] fix: locale parsing related crash on some mpv builds (#794) 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. --- src/uosc/lib/intl.lua | 7 ++++--- src/uosc/lib/std.lua | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/uosc/lib/intl.lua b/src/uosc/lib/intl.lua index 5e4d8d3d..79a7c64e 100644 --- a/src/uosc/lib/intl.lua +++ b/src/uosc/lib/intl.lua @@ -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 diff --git a/src/uosc/lib/std.lua b/src/uosc/lib/std.lua index 6c42e052..0797a498 100644 --- a/src/uosc/lib/std.lua +++ b/src/uosc/lib/std.lua @@ -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 @@ -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