diff options
author | William Boman <william@redwill.se> | 2022-05-01 21:08:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-01 21:08:05 +0200 |
commit | 069da468d5d2af01279a121473aef09c87b163aa (patch) | |
tree | 7b80d6fe78e44ed420e8a0be1c28158f96083274 | |
parent | 65b4bf055f58eb622f59188c3bace2519cb777b1 (diff) | |
download | rneovim-069da468d5d2af01279a121473aef09c87b163aa.tar.gz rneovim-069da468d5d2af01279a121473aef09c87b163aa.tar.bz2 rneovim-069da468d5d2af01279a121473aef09c87b163aa.zip |
fix(shared): avoid indexing unindexable values in vim.tbl_get() (#18337)
-rw-r--r-- | runtime/lua/vim/shared.lua | 5 | ||||
-rw-r--r-- | test/functional/lua/vim_spec.lua | 4 |
2 files changed, 8 insertions, 1 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index f0dc34608c..172fac3a88 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -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 diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index d9a8dfd2e8..73e4d7ca79 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -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) |