Skip to content

reflowSmaller panics with index out of range [-1] when reflowed lines exceed capacity聽#61

Description

@eugenioenko

馃 (Agent generated text, read next message first please)

Narrowing a buffer whose reflowed lines do not fit in the scrollback panics:

panic: runtime error: index out of range [-1]
  CircularList[...].Set      circularlist.go:84
  Buffer.reflowSmaller       buffer.go:449
  Buffer.Resize              buffer.go:252

Repro (fails on dae5128):

b := NewBuffer(BufferOptions{Cols: 20, Rows: 2, Scrollback: 2, TabStopWidth: 8, HasScrollback: true})
b.FillViewportRows(nil)
attrs := &AttributeData{Extended: &ExtendedAttrs{}}
for y := range b.Lines.Length() {
	for i, ch := range []rune("ABCDEFGHIJKLMNOPQRST") {
		b.Lines.Get(y).SetCellFromCodepoint(i, uint32(ch), 1, attrs)
	}
}
b.Resize(2, 2)

It also happens at realistic sizes: an 80x24 terminal with 1000 lines of scrollback full of long wrapped lines panics when resized to 40 columns.

Cause: in the rearrange loop of reflowSmaller (buffer.go, around line 448), the inner loop decrements the write index i once per inserted line but only checks nextI >= 0. If the reflow adds more lines than the list can hold, i goes below 0. Set then panics, or overwrites an unrelated slot if the list has wrapped.

Suggested fix: also bound the inner loop by i:

-for nextI := len(nextToInsert.newLines) - 1; nextI >= 0; nextI-- {
+for nextI := len(nextToInsert.newLines) - 1; nextI >= 0 && i >= 0; nextI-- {

A patch with a regression test is on this branch: https://github.com/eugenioenko/xterm-go/tree/fix/reflow-smaller-negative-index

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions