Skip to content

Commit cb472d6

Browse files
fix: eliminate data race in BufferLine bulk operations
The package-level workCell variable was shared across goroutines when parallel tests called InsertCells/DeleteCells concurrently, causing a data race detected by -race. Replace with stack-local CellData. Fixes #11 Co-authored-by: Ona <no-reply@ona.com>
1 parent 445513b commit cb472d6

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

bufferline.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ func (bl *BufferLine) AddCodepointToCell(index int, codePoint uint32, width int)
218218

219219
// --- Bulk operations ---
220220

221-
// workCell is a reusable CellData to avoid allocations in bulk operations.
222-
var workCell = NewCellData()
223-
224221
// InsertCells inserts n cells at pos, shifting existing cells right.
225222
// Cells that fall off the end are lost. New cells are filled with fillCell.
226223
func (bl *BufferLine) InsertCells(pos, n int, fillCell *CellData) {
@@ -230,8 +227,9 @@ func (bl *BufferLine) InsertCells(pos, n int, fillCell *CellData) {
230227
bl.SetCellFromCodepoint(pos-1, 0, 1, &fillCell.AttributeData)
231228
}
232229
if n < bl.Len-pos {
230+
var tmp CellData
233231
for i := bl.Len - pos - n - 1; i >= 0; i-- {
234-
bl.SetCell(pos+n+i, bl.LoadCell(pos+i, workCell))
232+
bl.SetCell(pos+n+i, bl.LoadCell(pos+i, &tmp))
235233
}
236234
for i := range n {
237235
bl.SetCell(pos+i, fillCell)
@@ -252,8 +250,9 @@ func (bl *BufferLine) InsertCells(pos, n int, fillCell *CellData) {
252250
func (bl *BufferLine) DeleteCells(pos, n int, fillCell *CellData) {
253251
pos %= bl.Len
254252
if n < bl.Len-pos {
253+
var tmp CellData
255254
for i := range bl.Len - pos - n {
256-
bl.SetCell(pos+i, bl.LoadCell(pos+n+i, workCell))
255+
bl.SetCell(pos+i, bl.LoadCell(pos+n+i, &tmp))
257256
}
258257
for i := bl.Len - n; i < bl.Len; i++ {
259258
bl.SetCell(i, fillCell)

0 commit comments

Comments
 (0)