summaryrefslogtreecommitdiff
path: root/combobox.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-03-09 10:41:07 -0400
committerPietro Gagliardi <[email protected]>2014-03-09 10:41:07 -0400
commit3e47b00eda59d3365bc189e5dc23fd0644e2d84a (patch)
tree4d6978473bbb71506960abc02eac8e38e694c257 /combobox.go
parenta67eaaf9babd5448a78a4887c0569b445932c83b (diff)
Added bounds checks for Combobox.InsertBefore() and Combobox.Delete().
Diffstat (limited to 'combobox.go')
-rw-r--r--combobox.go24
1 files changed, 21 insertions, 3 deletions
diff --git a/combobox.go b/combobox.go
index fbeb365..42e10ac 100644
--- a/combobox.go
+++ b/combobox.go
@@ -53,31 +53,49 @@ func (c *Combobox) Append(what ...string) (err error) {
return nil
}
-// InsertBefore inserts a new item in the Combobox before the item at the given position. (TODO action if before is out of bounds)
+// InsertBefore inserts a new item in the Combobox before the item at the given position. It panics if the given index is out of bounds.
func (c *Combobox) InsertBefore(what string, before int) (err error) {
c.lock.Lock()
defer c.lock.Unlock()
+ var m []string
+
if c.created {
+ if before < 0 || before >= c.sysData.len() {
+ goto badrange
+ }
return c.sysData.insertBefore(what, before)
}
- m := make([]string, 0, len(c.initItems) + 1)
+ if before < 0 || before >= len(c.initItems) {
+ goto badrange
+ }
+ m = make([]string, 0, len(c.initItems) + 1)
m = append(m, c.initItems[:before]...)
m = append(m, what)
c.initItems = append(m, c.initItems[before:]...)
return nil
+badrange:
+ panic(fmt.Errorf("index %d out of range in Combobox.InsertBefore()", before))
}
-// Delete removes the given item from the Combobox. (TODO action if index is out of bounds)
+// Delete removes the given item from the Combobox. It panics if the given index is out of bounds.
func (c *Combobox) Delete(index int) error {
c.lock.Lock()
defer c.lock.Unlock()
if c.created {
+ if index < 0 || index >= c.sysData.len() {
+ goto badrange
+ }
return c.sysData.delete(index)
}
+ if index < 0 || index >= len(c.initItems) {
+ goto badrange
+ }
c.initItems = append(c.initItems[:index], c.initItems[index + 1:]...)
return nil
+badrange:
+ panic(fmt.Errorf("index %d out of range in Combobox.Delete()", index))
}
// Selection returns the current selection.