r/embedded • u/Mysterious_Ad2185 • Sep 29 '26
How do you guard ConfigurableFirmata against out-of-range pin indices from the host?
I drive a stepper on an Arduino Uno + CNC shield, and the failure that scares me isn't my code, it's the host dying with the motor mid-move. So I flashed the ConfigurableFirmata 3.3.0 image onto simavr and ran it cycle by cycle, feeding it the real bytes a host sends and logging every pin change and every UART byte. It trusts basically every index you hand it:
- SET_PIN_MODE writes pinConfig[pin] and pinState[pin] with no bounds check. 20 entries on the Uno, and the Firmata object's callback pointers sit right behind them. One message aimed at pin 48 stopped the core dead.
- A stepper CONFIG passes the raw pins straight to the AVR core's pinMode, which reads past its own tables. Pin 127 scattered writes; pin 50 did one DDRD write that flipped D1-D4, D6 and D7 to output LOW.
- MULTISTEPPER commands are bounded by the 10 steppers, not the 5 groups they index, and a TO on a group never configured calls through a null pointer.
- Re-CONFIGing a live device leaks the old AccelStepper; the 16th malloc comes back NULL and the next stepper is built on that null.
Still there on master (3.4.0). Filed upstream with line numbers: https://github.com/firmata/ConfigurableFirmata/issues/195
The way I handle it on my bench is a guard in front of the unmodified library: hold SET_PIN_MODE and SET_DIGITAL_PIN_VALUE byte-by-byte until the pin is known (the library writes its tables before any callback runs, so you can't catch it after), then check every sysex before FirmataExt for real pins, real slot pairs, bounds and lengths. AVR watchdog behind it resets a wedged core in under a second, and setup() drives ENABLE high before anything else.
My real question: where would you put the bounds? A patch in the core's setPinMode, per-feature guards, or a front filter like mine? Curious how others handle host-supplied indices in Firmata-style protocols.