Leônidas Augusto Amaro

One missing ampersand: a heap-corruption crash in the Bruce firmware

3 min readDebuggingEmbeddedOpen source

Bruce is open-source firmware for ESP32 devices, with more than 7,000 stars on GitHub. In May, a user opened issue #2477: in RF→Scan/Copy, after capturing a signal with a CC1101 radio, pressing NEXT to open the options menu crashed the device and rebooted it. The reporter added one useful detail: the previous version didn't do this.

I had the same hardware, an M5Stack Cardputer with a CC1101 module, so I could reproduce it. It failed every single time.

Read the line before the assert

The crash log ended like this:

E (26074) gpio: gpio_isr_handler_remove(573): GPIO number error
E (26075) gpio: gpio_wakeup_disable(679): GPIO number error
 
assert failed: heap_caps_free heap_caps_base.c:80
(heap != NULL && "free() target pointer is outside heap areas")

The assert is the loud part, but it is only where the damage was noticed. The two lines above it say what went wrong first: something asked the GPIO driver to remove an interrupt handler from a pin number that doesn't exist.

So the question became: who removes an interrupt when you press NEXT?

Follow the interrupt

Pressing NEXT calls select_menu_option(), which stops the receiver with rcswitch.disableReceive(). In the RCSwitch library, disableReceive() calls detachInterrupt(nReceiverInterrupt), and it doesn't check the value first.

nReceiverInterrupt starts at -1 and only gets a real pin when enableReceive() runs. On ESP32, detachInterrupt(-1) indexes the GPIO interrupt table out of bounds and then calls free() on whatever it finds there. That is the heap corruption in the log.

Which meant enableReceive() had never run on this object. But the code clearly called it.

The copy

Here is the helper that enables the receiver:

void RFScan::RCSwitch_Enable_Receive(RCSwitch rcswitch) {
    if (bruceConfigPins.rfModule == CC1101_SPI_MODULE) {
        rcswitch.enableReceive(bruceConfigPins.CC1101_bus.io0);
    } else {
        // ...
    }
}

rcswitch is taken by value. The function enables a temporary copy, and the copy is thrown away when the function returns. The RFScan::rcswitch member, the one that NEXT later disables, still has nReceiverInterrupt == -1. Nothing fails at that point. The bug only shows up later, in a different screen, as a heap assert.

The fix

Take the object by reference, so enabling and disabling act on the same object:

- void RCSwitch_Enable_Receive(RCSwitch rcswitch);
+ void RCSwitch_Enable_Receive(RCSwitch &rcswitch);

The same change goes in the header and in the .cpp file: two lines, one character each.

On the device, capture followed by NEXT now opens the options menu, many times in a row, with no reboot. Capturing, saving and replaying signals still work. I opened the pull request with the reproduction, the root cause and the test, and it was merged about 14 hours later.

What I took from it

  • In C++, passing a stateful object by value is a quiet trap. The copy works, the compiler is happy, and the bug surfaces far from where it was written. Objects that own a handle, an interrupt or a connection almost always want a reference.
  • The useful line in a crash log is often the error just before the assert. The assert tells you where the damage was found. The errors above it tell you what caused it.
  • A small fix gets merged fast when the pull request answers every question: how to reproduce it, why it happens, what changed and how it was tested. The maintainers didn't need to ask anything.

All posts