From ee382285e763b3376ff85acd17bbbc059272f5b1 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 c2419c8..9e7bbc6 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 } }