Skip to content

fix: guard setCursor against nil and fallback getNextCol on regex miss#2

Open
vann-at-cpcp-dot-tw wants to merge 1 commit into
colomb8:mainfrom
vann-at-cpcp-dot-tw:fix-setcursor-nil-guard
Open

fix: guard setCursor against nil and fallback getNextCol on regex miss#2
vann-at-cpcp-dot-tw wants to merge 1 commit into
colomb8:mainfrom
vann-at-cpcp-dot-tw:fix-setcursor-nil-guard

Conversation

@vann-at-cpcp-dot-tw
Copy link
Copy Markdown

Summary

Fixes a crash in insert-mode motions when the cursor is near certain multi-byte / edge-case characters.

Reproduction

Open a markdown file, paste text containing CJK or special characters, then press <Right> / <Left> in insert mode. Get:

```
E5108: Error executing lua: ...lua/rambo/core.lua:94: attempt to perform arithmetic on local 'col' (a nil value)
stack traceback:
...lua/rambo/core.lua:94: in function 'setCursor'
...lua/rambo/core.lua:533: in function 'move'
...lua/rambo/core.lua:1169: in function <...>
```

Root cause

`getNextCol` uses `vim.regex("\\%Xc.")` which can miss at edge byte positions, returning `nil` via `return to and to + 1`. `setCursor` then does `col - 1` on nil → crash.

Fix

  1. `setCursor`: early-return when `row` or `col` is `nil` (defense-in-depth — covers every motion path that may pass nil through).
  2. `getNextCol`: fallback to the naive `col + 1` on regex miss. This matches the commented-out `col_target = col + 1` intent visible in `rmbMotionRight` — cursor still moves instead of becoming no-op.

`getPrevCol` returning `nil` at line-start is left alone — it's a meaningful boundary; the `setCursor` guard makes it safe.

Test plan

  • Before patch: paste CJK text in insert mode, press Right → crash
  • After patch: cursor advances normally, no crash
  • Regular ASCII motion unchanged

🤖 Generated with Claude Code

When editing text containing certain multi-byte or edge-case characters,
pressing insert-mode motion keys (e.g. Right arrow) could crash with:

  E5108: attempt to perform arithmetic on local 'col' (a nil value)
         at setCursor (core.lua:94)
         called from rmbMotionRight.move (core.lua:533)

Root cause:
- getNextCol uses vim.regex("\\%Xc.") which can miss in edge cases,
  returning `nil` via `return to and to + 1`.
- setCursor then does `col - 1` on nil and throws.

Fix:
1. setCursor: early-return when row/col is nil (defense-in-depth,
   covers all motion paths that pass nil through).
2. getNextCol: fallback to the naive `col + 1` on regex miss
   (matches the author's own commented-out `col_target = col + 1`
   intent at rmbMotionRight, keeps cursor moving instead of no-op).
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.

1 participant