From c43583fe20a3a54920774fb762c74d22b60b70dc Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 11 Mar 2014 13:37:19 -0400 Subject: [PATCH] Handle out of range on Listbox.Delete(). The Mac OS X exception behavior I previously noted has bene resolved: what happens after exception handling is undefined :| --- listbox.go | 11 +++++++++-- test/main.go | 9 +++++++-- todo.md | 2 -- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/listbox.go b/listbox.go index 6d5decc..63f8722 100644 --- a/listbox.go +++ b/listbox.go @@ -69,17 +69,24 @@ badrange: panic(fmt.Errorf("index %d out of range in Listbox.InsertBefore()", before)) } -// Delete removes the given item from the Listbox. -// (TODO action on invalid index) +// Delete removes the given item from the Listbox. It panics if the given index is out of bounds. func (l *Listbox) Delete(index int) error { l.lock.Lock() defer l.lock.Unlock() if l.created { + if index < 0 || index >= l.sysData.len() { + goto badrange + } return l.sysData.delete(index) } + if index < 0 || index >= len(l.initItems) { + goto badrange + } l.initItems = append(l.initItems[:index], l.initItems[index + 1:]...) return nil +badrange: + panic(fmt.Errorf("index %d out of range in Listbox.Delete()", index)) } // Selection returns a list of strings currently selected in the Listbox, or an empty list if none have been selected. This list will have at most one item on a single-selection Listbox. diff --git a/test/main.go b/test/main.go index 911dffa..6d0a5c0 100644 --- a/test/main.go +++ b/test/main.go @@ -57,7 +57,7 @@ func invalidTest(c *Combobox, l *Listbox) { defer x("Combobox.Delete < 0"); c.Delete(-5); panic(nil) }() func() { - defer x("Listbox.Delete > len"); c.Delete(c.Len() + 5); panic(nil) + defer x("Combobox.Delete > len"); c.Delete(c.Len() + 5); panic(nil) }() func() { defer x("Listbox.InsertBefore < 0"); l.InsertBefore("xxx", -5); panic(nil) @@ -65,7 +65,12 @@ func invalidTest(c *Combobox, l *Listbox) { func() { defer x("Listbox.InsertBefore > len"); l.InsertBefore("xxx", l.Len() + 5); panic(nil) }() - // TODO add Listbox.Delete() tests when I move that over + func() { + defer x("Listbox.Delete < 0"); l.Delete(-5); panic(nil) + }() + func() { + defer x("Listbox.Delete > len"); l.Delete(c.Len() + 5); panic(nil) + }() MsgBox("test", "all working as intended") } diff --git a/todo.md b/todo.md index 5f69cfe..ec02400 100644 --- a/todo.md +++ b/todo.md @@ -70,8 +70,6 @@ super ultra important things: (test:17575): Gtk-CRITICAL **: gtk_device_grab_remove: assertion 'GDK_IS_DEVICE (device)' failed ``` figure out why -- Mac OS X 32-bit builds don't call runtime.sighandler() on a Cocoa exception like the 64-bit builds do - - does array index out of bounds on 32-bit Cocoa not generate a SIGTRAP like invalid selector does? need to experiment... or is something clobbering SIGTRAP? I didn't catch it in my tests here... - the user can still [NSApp terminate:] from the Dock icon, bypassing Go itself - ideally we need a QuitItem() function for this case if/when we add menus