Skip to content

Fix selection crashes and scrollback handling (0.9.4)#4

Open
kdedev wants to merge 2 commits intomarchaesen:mainfrom
kdedev:lazy-history-allocation
Open

Fix selection crashes and scrollback handling (0.9.4)#4
kdedev wants to merge 2 commits intomarchaesen:mainfrom
kdedev:lazy-history-allocation

Conversation

@kdedev
Copy link

@kdedev kdedev commented Feb 10, 2026

This PR fixes several critical bugs in selection handling during scrollback:

Crashes fixed:

  • Fix crash when copying selection while viewing scrollback history
  • Fix use-after-free in datasrccancelled when clipboard source changes
  • Add bounds validation in getsel() to prevent out-of-bounds memory access

Selection improvements:

  • Selection now uses scrollback-relative coordinates instead of screen-relative
  • Mouse wheel scroll during active selection preserves the selection (like Windows terminals)
  • New kscrollup_nosel/kscrolldown_nosel functions for selection-preserving scroll

Other improvements:

  • Lazy page-based history allocation (original PR scope)
  • Increase default scroll lines from 1 to 3 for smoother scrolling
  • Add PageUp/PageDown keyboard support with proper key release handling

Tested extensively with ASAN - no memory errors detected.

Instead of allocating all 10000 history lines upfront (~3.8 MB),
allocate them lazily in pages of 256 lines as scrollback is used.

A typical terminal session only scrolls back a few hundred lines,
so most of the history buffer is never touched. This avoids wasting
memory on unused allocations.

Changes:
- HISTSIZE increased from 2000 to 10000 (cheap with lazy alloc)
- Added HIST_PAGESIZE (256) for batch allocation granularity
- Added histensure() to allocate a page of history on first access
- tnew initializes hist[] to NULL instead of xmalloc
- tresize and clear/reset guard against NULL hist entries
- Both st.c (non-reflow) and patch/reflow.c paths covered
@kdedev kdedev changed the title scrollback: lazy page-based history allocation Fix selection crashes and scrollback handling (0.9.4) Feb 12, 2026
@kdedev kdedev force-pushed the lazy-history-allocation branch from 696d2c4 to dde512b Compare February 12, 2026 09:02
….9.5)

Selection system overhaul:
- Scroll-while-selecting: dragging past top/bottom edge auto-scrolls
  using nosel variants that preserve selection during scroll
- Fix selection coordinate drift: keep coords screen-relative (removed
  erroneous + term.scr offset in selstart/selextend)
- Fix selection clearing: only clear when both endpoints scroll fully
  off-screen in the same direction
- Fix scrollback copy: getsel clamps to full TLINE range
  (0..term.row+term.scr-1), copies entire selected region
- Fix selnormalize crash: nosel functions use direct MIN/MAX on nb/ne
  instead of calling selnormalize (selsnap unsafe with off-screen y)
- Add DBGSEL macro for conditional debug logging (zero cost in release)

Scroll behavior:
- Mouse wheel: 3 lines per tick, natural scroll direction
- Plain PageUp/PageDown passed to apps (micro, vim, etc.)
- Shift+PageUp/Down for terminal scrollback
- Bounds validation on all selection coordinates
@kdedev kdedev force-pushed the lazy-history-allocation branch from dde512b to 8232a90 Compare February 12, 2026 09:33
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