From 62d2cb4f2c90dfc6847cdf59633070f2b6bc743d Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Sun, 5 Feb 2023 11:54:08 +0100 Subject: [PATCH] fix(textarea): fix data corruption after multi-line input Previously, when inserting multiple lines of input at once, the underlying Go data array was incorrectly shared in memory across multiple rows of the textarea. This was causing input corruption, where a user trying to modify one line would see their text added on the next line as well. This patch fixes it. --- textarea/textarea.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/textarea/textarea.go b/textarea/textarea.go index c2419c8e..9e7bbc60 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -321,7 +321,11 @@ func (m *Model) insertRunesFromUserInput(runes []rune) { lstart := 0 for i := 0; i < len(runes); i++ { if runes[i] == '\n' { - lines = append(lines, runes[lstart:i]) + // Queue a line to become a new row in the text area below. + // Beware to clamp the max capacity of the slice, to ensure no + // data from different rows get overwritten when later edits + // will modify this line. + lines = append(lines, runes[lstart:i:i]) lstart = i + 1 } }