Skip to content

fix(render): guard toPos / lineWrap against zero terminal column count#290

Open
SAY-5 wants to merge 1 commit intoc-bata:masterfrom
SAY-5:fix/render-col-zero-divide-277
Open

fix(render): guard toPos / lineWrap against zero terminal column count#290
SAY-5 wants to merge 1 commit intoc-bata:masterfrom
SAY-5:fix/render-col-zero-divide-277

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 21, 2026

What

Fixes #277.

Render.toPos divided cursor by r.col without a zero check. When the terminal reported a zero column count - UpdateWinSize not yet called, CI runner with no TTY, input piped in, or ioctl returning zero for any other reason - the first keystroke path hit panic: runtime error: integer divide by zero and crashed the host process.

Fix

  • toPos: clamp col to a minimum of 1 so the prompt still renders on the first row.
  • lineWrap: add the same guard before the cursor % r.col == 0 check so the newline branch cannot fire the same crash.

Once the real window size arrives via SIGWINCH or a manual UpdateWinSize call, subsequent keystrokes behave exactly as before.

Verification

Locally on macOS, go 1.26.2:

  • gofmt -s -l: clean
  • go vet ./...: clean
  • go test -race -count=1 ./...: all packages pass

Closes #277

Render.toPos divided cursor by r.col without a zero-check. When the
terminal reported a zero size (UpdateWinSize not called yet, the
process runs under a CI runner with no TTY, input is piped in, or the
ioctl returns zero for any other reason), the first keystroke path
hit 'panic: runtime error: integer divide by zero' and crashed the
host process (c-bata#277).

Clamp col to a minimum of 1 in toPos so the prompt can still render
on the first row, and skip the modulo in lineWrap when r.col is
non-positive so the same crash path cannot fire from the newline
branch either. Once the real window size arrives via SIGWINCH or
UpdateWinSize, subsequent keystrokes behave exactly as before.

Closes c-bata#277

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
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.

[Bug] panic: runtime error: integer divide by zero

1 participant