Skip to content

Bugfix: Unsafe check on cursorword_id#30

Open
ibrahimmkhalid wants to merge 2 commits intoya2s:mainfrom
ibrahimmkhalid:bugfix-unsafe-check-on-cursorword-id
Open

Bugfix: Unsafe check on cursorword_id#30
ibrahimmkhalid wants to merge 2 commits intoya2s:mainfrom
ibrahimmkhalid:bugfix-unsafe-check-on-cursorword-id

Conversation

@ibrahimmkhalid
Copy link

Issue

If ":call clearmatches()" was performed, it would break the cursorword highlighting for that window

Discovery

I discovered this bug while installing and testing out ThePrimeagen's latest harpoon update. When opening the harpoon menu, the clearmatches() function was called. This would cause an error message to pop up anytime the cursor moved in the popup menu. Closing the popup menu would fix it in the open file however.

I believe this is a bug in this plugin as there is a check on the existence of cursorword_id on the w object, but that does not take into account whether that that id actually gets deleted.

How to reproduce (discovery)

  1. minimal init.lua: https://pastebin.com/DUp8H0Yx
  2. open a file with some text greater than 3 characters
  3. move cursor over to that word
  4. open quick menu with mm
  5. error!

How to reproduce (general case)

  1. open a file with some text greater than 3 characters
  2. move cursor over to that word
  3. do ":call clearmatches()"
  4. error!

Fix

Move deletion into a pcall function and retain previous code flow with the if check.

p.s. I am new to open source development. If there's anything i can fix about my post, please let me know. I would really appreciate it

@ya2s
Copy link
Owner

ya2s commented Feb 13, 2026

Thanks for the fix proposal and clear repro steps. I reviewed this patch and found a few issues that likely prevent it from fixing the root cause:

  1. pcall return handling is incorrect.
  • Current code uses "_, ok = pcall(...)" and then checks "if not ok then".
  • In Lua, the first return from pcall is the success boolean.
  • With the current assignment, ok is not the success flag, so failure/success detection is unreliable.
  1. The "if w.cursorword_id then" guard was removed.
  • This causes an unconditional delete attempt even when no match id is stored.
  • It is safer to keep the original guard.
  1. ok is not local.
  • "_, ok = ..." introduces/uses a global variable, which we should avoid.

Suggested minimal fix:

if w.cursorword_id then
  pcall(vim.call, "matchdelete", w.cursorword_id)
  w.cursorword_id = nil
end

(Equivalent vim.fn.matchdelete variant is also fine.)

Also, adding a regression test for the ":call clearmatches()" scenario would make this much safer to merge.

@ibrahimmkhalid
Copy link
Author

pulled in changes from upstream and incorporated the pcall into the clear_cursorword_match function

@ibrahimmkhalid ibrahimmkhalid marked this pull request as draft February 16, 2026 02:35
@ibrahimmkhalid ibrahimmkhalid marked this pull request as ready for review February 22, 2026 04:32
@ibrahimmkhalid
Copy link
Author

Also, adding a regression test for the ":call clearmatches()" scenario would make this much safer to merge.

Since there is no testing setup in the repo as of now, I did not add explicit tests, however I tested it on my system and it seems to be working without any issues. Let me know if you find anything that needs fixing or any other comments and I will try incorporating them. Thanks!

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