mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
[Backport release-0.7] fix(shared): avoid indexing unindexable values in vim.tbl_get() (#18360)
* fix(shared): avoid indexing unindexable values in vim.tbl_get() (cherry picked from commit95ec38dc8d
) * test: add more unindexable types (cherry picked from commit7dccdefce4
) Co-authored-by: William Boman <william@redwill.se>
This commit is contained in:
committed by
GitHub
parent
508c8a597e
commit
08cd391047
@ -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
|
||||
|
@ -492,6 +492,10 @@ 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({ 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({})"))
|
||||
end)
|
||||
|
Reference in New Issue
Block a user