fix(shared): avoid indexing unindexable values in vim.tbl_get()

This commit is contained in:
William Boman committed 2022-05-01 01:32:53 +02:00
1 parent cc27540560
commit 95ec38dc8d
2 files changed
+6 -1

No files matched your search

+4 -1
View File
@@ -365,7 +365,10 @@ function vim.tbl_get(o, ...)
if #keys == 0 then
return
end
for _, k in ipairs(keys) do
for i, k in ipairs(keys) do
if type(o[k]) ~= 'table' and next(keys, i) then
return nil
end
o = o[k]
if o == nil then
return
+2
View File
@@ -492,6 +492,8 @@ describe('lua stdlib', function()
it('vim.tbl_get', function()
eq(true, exec_lua("return vim.tbl_get({ test = { nested_test = true }}, 'test', 'nested_test')"))
eq(NIL, exec_lua("return vim.tbl_get({ unindexable = true }, 'unindexable', 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({ unindexable = 1 }, 'unindexable', 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({})"))
end)