← KD100

Fix crash when printing incomplete set debug info

516c2b05d71225251a64473f02390481c08cae07 · Claude · Jan 22, 2026 20:37

When switching to an incomplete set (e.g., Set 3 with only function 4
but missing function 5), the debug print tried to access
wheelEvents[5] which is out of bounds, causing a segfault.

Crash scenario:
- User has 5 wheel functions (totalWheels = 5)
- Triple-clicks to Set 3, single-clicks to position 1
- wheelFunction = (2 * 2) + 1 = 5 (out of bounds!)
- Debug print tries to access wheelEvents[5].left → CRASH

Fix:
Add bounds check before accessing wheelEvents array in debug print.
If wheelFunction is out of bounds, print "(not defined - incomplete set)"
instead of crashing.

Note: The wheel turn handler already has proper bounds checking, so
turning the wheel when on an incomplete set position works fine (just
does nothing). Only the debug print was missing the check.

Files modified:
- src/device.c: Add bounds check to debug print statement

+7 −3 · 1 file changed

  • src/device.c +7 −3
diff --git a/src/device.c b/src/device.c
index d2cd5eb..dc87f3f 100644
--- a/src/device.c
+++ b/src/device.c
@@ -356,9 +356,13 @@ void device_run(libusb_context* ctx, config_t* config, int debug, int accept, in
                             if (debug == 1) {
                                 printf("Set: %d | Position: %d | Wheel Function: %d\n",
                                        wheel_current_set, wheel_position_in_set, wheelFunction);
-                                printf("Function: %s | %s\n",
-                                       config->wheelEvents[wheelFunction].left ? config->wheelEvents[wheelFunction].left : "(null)",
-                                       config->wheelEvents[wheelFunction].right ? config->wheelEvents[wheelFunction].right : "(null)");
+                                if (wheelFunction >= 0 && wheelFunction < config->totalWheels) {
+                                    printf("Function: %s | %s\n",
+                                           config->wheelEvents[wheelFunction].left ? config->wheelEvents[wheelFunction].left : "(null)",
+                                           config->wheelEvents[wheelFunction].right ? config->wheelEvents[wheelFunction].right : "(null)");
+                                } else {
+                                    printf("Function: (not defined - incomplete set)\n");
+                                }
                             }
                         }
                     }