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

This commit is contained in:
William Boman 2022-05-01 21:08:05 +02:00 committed by GitHub
parent 65b4bf055f
commit 069da468d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -365,7 +365,10 @@ function vim.tbl_get(o, ...)
if #keys == 0 then if #keys == 0 then
return return
end 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] o = o[k]
if o == nil then if o == nil then
return return

View File

@ -492,6 +492,10 @@ describe('lua stdlib', function()
it('vim.tbl_get', function() it('vim.tbl_get', function()
eq(true, exec_lua("return vim.tbl_get({ test = { nested_test = true }}, 'test', 'nested_test')")) 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({ unindexable = coroutine.create(function () end) }, 'unindexable', 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({ unindexable = function () end }, 'unindexable', 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')")) eq(NIL, exec_lua("return vim.tbl_get({}, 'missing_key')"))
eq(NIL, exec_lua("return vim.tbl_get({})")) eq(NIL, exec_lua("return vim.tbl_get({})"))
end) end)