Add bounds validation

This commit is contained in:
Daisuke Maki 2026-02-20 20:58:00 +09:00
parent 717dfca53b
commit 136b4a80e9
2 changed files with 17 additions and 3 deletions

View file

@ -40,16 +40,20 @@ func (q *Text) SaveQuery() {
func (q *Text) DeleteRange(start, end int) {
q.mutex.Lock()
defer q.mutex.Unlock()
if start == -1 {
if start < 0 {
return
}
l := len(q.query)
if start > l {
return
}
if end > l {
end = l
}
if start > end {
if start >= end {
return
}
@ -94,7 +98,12 @@ func (q *Text) InsertAt(ch rune, where int) {
q.mutex.Lock()
defer q.mutex.Unlock()
if where == len(q.query) {
l := len(q.query)
if where < 0 || where > l {
return
}
if where == l {
q.query = append(q.query, ch)
return
}

View file

@ -97,6 +97,9 @@ func TestQueryDeleteRange(t *testing.T) {
{"delete all", "abcdef", 0, 6, ""},
{"delete single char", "abcdef", 2, 3, "abdef"},
{"start is -1 (no-op)", "abcdef", -1, 3, "abcdef"},
{"start is negative (no-op)", "abcdef", -5, 3, "abcdef"},
{"start beyond length (no-op)", "abcdef", 10, 15, "abcdef"},
{"start equals end (no-op)", "abcdef", 3, 3, "abcdef"},
{"start > end (no-op)", "abcdef", 4, 2, "abcdef"},
{"end beyond length (clamped)", "abcdef", 4, 100, "abcd"},
{"unicode delete", "あいうえお", 1, 3, "あえお"},
@ -185,6 +188,8 @@ func TestQueryInsertAt(t *testing.T) {
{"insert in middle", "hello", 'X', 2, "heXllo"},
{"insert into empty", "", 'X', 0, "X"},
{"insert unicode", "hello", 'あ', 2, "heあllo"},
{"insert at negative index (no-op)", "hello", 'X', -1, "hello"},
{"insert beyond length (no-op)", "hello", 'X', 10, "hello"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {