Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential nil access #821

Merged
merged 2 commits into from
Oct 2, 2023
Merged

Fix potential nil access #821

merged 2 commits into from
Oct 2, 2023

Conversation

dsully
Copy link
Contributor

@dsully dsully commented Sep 24, 2023

If one uses this method mapped to keys and accidentally hit a number with a non-existing buffer, element can be nil:

E5108: Error executing lua: ...re/nvim/lazy/bufferline.nvim/lua/bufferline/commands.lua:122: attempt to index local 'element' (a nil value)
stack traceback:
        ...re/nvim/lazy/bufferline.nvim/lua/bufferline/commands.lua:122: in function 'go_to'

If one uses this method mapped to keys and accidentally hit a number with a non-existing buffer, `element` can be nil:

```
E5108: Error executing lua: ...re/nvim/lazy/bufferline.nvim/lua/bufferline/commands.lua:122: attempt to index local 'element' (a nil value)
stack traceback:
        ...re/nvim/lazy/bufferline.nvim/lua/bufferline/commands.lua:122: in function 'go_to'
```
@@ -119,7 +119,10 @@ function M.go_to(num, absolute)
local list = absolute and state.components or state.visible_components
local element = list[num]
if num == -1 or not element then element = list[#list] end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused why this check wouldn't be enough already? if element is falsy then it should use the last item in the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is only 1 buffer, it appears that there isn't any items in the list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I insert:

vim.notify(vim.inspect(element))
vim.notify(vim.inspect(list))

in between lines 120 and 121, the output is nil and {} respectively.

Copy link
Owner

@akinsho akinsho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏾

@akinsho akinsho merged commit 6e96fa2 into akinsho:main Oct 2, 2023
sstallion pushed a commit to sstallion/bufferline.nvim that referenced this pull request Jul 3, 2024
* Fix potential nil access

If one uses this method mapped to keys and accidentally hit a number with a non-existing buffer, `element` can be nil:

```
E5108: Error executing lua: ...re/nvim/lazy/bufferline.nvim/lua/bufferline/commands.lua:122: attempt to index local 'element' (a nil value)
stack traceback:
        ...re/nvim/lazy/bufferline.nvim/lua/bufferline/commands.lua:122: in function 'go_to'
```

* Update lua/bufferline/commands.lua

Co-authored-by: Akin <[email protected]>

---------

Co-authored-by: Akin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants