I have a mouse with dedicated back and forward buttons and I noticed that all the examples panic when I press the "back" button (which seems like it's received as "mouse button 9").
This is a backtrace I got from the simple_polyline example:
thread 'main' panicked at 'index out of bounds: the len is 9 but the index is 9', src/state.rs:224:13
stack backtrace:
0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
1: std::sys_common::backtrace::_print
at src/libstd/sys_common/backtrace.rs:71
2: std::panicking::default_hook::{{closure}}
at src/libstd/sys_common/backtrace.rs:59
at src/libstd/panicking.rs:197
3: std::panicking::default_hook
at src/libstd/panicking.rs:211
4: std::panicking::rust_panic_with_hook
at src/libstd/panicking.rs:474
5: std::panicking::continue_panic_fmt
at src/libstd/panicking.rs:381
6: rust_begin_unwind
at src/libstd/panicking.rs:308
7: core::panicking::panic_fmt
at src/libcore/panicking.rs:85
8: core::panicking::panic_bounds_check
at src/libcore/panicking.rs:61
9: nannou::state::mouse::ButtonMap<S>::press
at src/state.rs:224
10: nannou::app::process_and_emit_winit_event
at src/app.rs:1831
11: nannou::app::poll_and_process_events
at src/app.rs:1681
12: nannou::app::run_loop_mode_refresh_sync
at src/app.rs:1490
13: nannou::app::run_loop
at src/app.rs:1251
14: nannou::app::Builder<M,E>::run
at src/app.rs:636
15: nannou::app::Builder::sketch
at src/app.rs:666
16: nannou::sketch
at src/lib.rs:76
17: simple_polyline::main
at examples/simple_polyline.rs:4
18: std::rt::lang_start::{{closure}}
at /rustc/0ff088c9ee6a132daf256383e56004bee34fbaae/src/libstd/rt.rs:64
19: std::panicking::try::do_call
at src/libstd/rt.rs:49
at src/libstd/panicking.rs:293
20: __rust_maybe_catch_panic
at src/libpanic_unwind/lib.rs:85
21: std::rt::lang_start_internal
at src/libstd/panicking.rs:272
at src/libstd/panic.rs:388
at src/libstd/rt.rs:48
22: std::rt::lang_start
at /rustc/0ff088c9ee6a132daf256383e56004bee34fbaae/src/libstd/rt.rs:64
23: main
24: __libc_start_main
25: _start
In src/state.rs the maximum number of buttons on a mouse is assumed to be 9:
https://github.com/nannou-org/nannou/blob/a5486e12b425df14cf2bb2c04496b5d3c38e1854/src/state.rs#L80-L81
With this constant, a fixed-size array is created to hold all the button states:
https://github.com/nannou-org/nannou/blob/a5486e12b425df14cf2bb2c04496b5d3c38e1854/src/state.rs#L108-L111
This function generates the array index from a mouse button event:
https://github.com/nannou-org/nannou/blob/a5486e12b425df14cf2bb2c04496b5d3c38e1854/src/state.rs#L273-L280
However, in multiple places this function is used to directly access the fixed-size array:
https://github.com/nannou-org/nannou/blob/a5486e12b425df14cf2bb2c04496b5d3c38e1854/src/state.rs#L222-L246
Since the button-ID reported seems to be 9, the index 9 is out of bounds.
Either the fixed-size array could be replaced by a Vec<ButtonPosition<S>> and initialized to N button states. Then when a new button-index is encountered this array could be resized lazily and filled with default states.
Maybe another solution is to have button_to_idx return an Option<usize> instead, any unexpected button input could then be discarded.
Hi @karroffel thanks heaps for bringing this to our attention and for the detailed report.
I actually have a new fancy mouse with heaps of buttons arriving from DHL any minute now so i'll look into addressing this and submit a PR hopefully within the next few days.
Most helpful comment
Hi @karroffel thanks heaps for bringing this to our attention and for the detailed report.
I actually have a new fancy mouse with heaps of buttons arriving from DHL any minute now so i'll look into addressing this and submit a PR hopefully within the next few days.