diff options
| author | Pietro Gagliardi <[email protected]> | 2014-03-09 10:51:01 -0400 |
|---|---|---|
| committer | Pietro Gagliardi <[email protected]> | 2014-03-09 10:51:01 -0400 |
| commit | f4506277b94bc2bff4790b6e7cee543b91c44fce (patch) | |
| tree | c470abac6e0b399ff01393f7033d997ff9b5ae7d /listbox.go | |
| parent | 3e47b00eda59d3365bc189e5dc23fd0644e2d84a (diff) | |
Added bounds checks for Listbox.InsertBefore(). This stupid 32-bit Mac bug keeps Listbox.Delete() unchecked for now...
Diffstat (limited to 'listbox.go')
| -rw-r--r-- | listbox.go | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -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. |
