From f4506277b94bc2bff4790b6e7cee543b91c44fce Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 9 Mar 2014 10:51:01 -0400 Subject: Added bounds checks for Listbox.InsertBefore(). This stupid 32-bit Mac bug keeps Listbox.Delete() unchecked for now... --- listbox.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'listbox.go') diff --git a/listbox.go b/listbox.go index 66ff398..54c698b 100644 --- a/listbox.go +++ b/listbox.go @@ -45,20 +45,29 @@ func (l *Listbox) Append(what ...string) (err error) { return nil } -// InsertBefore inserts a new item in the Listbox before the item at the given position. -// (TODO action on invalid index) +// InsertBefore inserts a new item in the Listbox before the item at the given position. It panics if the given index is out of bounds. func (l *Listbox) InsertBefore(what string, before int) (err error) { l.lock.Lock() defer l.lock.Unlock() + var m []string + if l.created { + if before < 0 || before >= l.sysData.len() { + goto badrange + } return l.sysData.insertBefore(what, before) } - m := make([]string, 0, len(l.initItems) + 1) + if before < 0 || before >= len(l.initItems) { + goto badrange + } + m = make([]string, 0, len(l.initItems) + 1) m = append(m, l.initItems[:before]...) m = append(m, what) l.initItems = append(m, l.initItems[before:]...) return nil +badrange: + panic(fmt.Errorf("index %d out of range in Listbox.InsertBefore()", before)) } // Delete removes the given item from the Listbox. -- cgit v1.2.3