[Bug] rgblight writes out-of-range LED indices on split keyboards, corrupting RAM (EEPROM cache on RP2040)
Describe the Bug
On split keyboards with RGBLIGHT_SPLIT, rgblight writes to out-of-range LED indices on the half whose clipping range does not start at 0. On RP2040 this corrupts unrelated RAM; in my case it was the wear-leveling cache that backs the emulated EEPROM, so VIA keymap entries changed by themselves while the keyboard was running.
Root cause
rgblight_led_index() subtracts clipping_start_pos and returns uint8_t:
uint8_t rgblight_led_index(uint8_t index) {
#if defined(RGBLIGHT_LED_MAP)
return pgm_read_byte(&led_map[index]) - rgblight_ranges.clipping_start_pos;
#else
return index - rgblight_ranges.clipping_start_pos;
#endif
}The effects, lighting layers and rgblight_set() loop over the whole effect range, which on a split board also includes the LEDs of the other half. On the right half of an 8-LED board (RGBLED_SPLIT {4, 4}, so clipping_start_pos = 4), the left LEDs 0..3 give 0 - 4 .. 3 - 4, which wraps to 252..255. That value goes straight to rgblight_driver.set_color() (in setrgb() and in 7 direct calls). The RP2040 vendor WS2812 driver writes it without a bounds check:
void ws2812_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
ws2812_leds[index].r = red; // ws2812_leds has WS2812_LED_COUNT (8) entries
...The LED colors therefore land about 750 bytes past ws2812_leds, inside the wear_leveling struct.
How it was confirmed
- Reading the VIA dynamic keymap (
id_dynamic_keymap_get_buffer) every few seconds showed the same 16-byte window changing continuously. The changing bytes were 4 RGB triplets, for example23 04 2d,1b 03 23,15 02 1b, all with the same hue at different brightness: the 4 LEDs of one half under the Twinkle effect. - Turning the strip off (VIA effect 0) stopped the corruption immediately. Turning it back on made it resume.
- Wrapping the driver with a bounds check (
RGBLIGHT_DRIVER = custom, forwarding tows2812_set_color()only whenindex < WS2812_LED_COUNT) removes the problem.
Expected behavior
Each half should only write the LEDs in its own clipping range. Indices that belong to the other half should never reach the driver.
A fix that does this is in the linked pull request: a bounds check in setrgb(), with every direct rgblight_driver.set_color(rgblight_led_index(...)) call routed through it.
Keyboard Used
sofle/rev1 with CONVERT_TO=kb2040 (hand-wired Raspberry Pi Pico, RP2040), RGBLIGHT_LED_COUNT 8, RGBLED_SPLIT {4, 4}, RGBLIGHT_LED_MAP {3, 2, 1, 0, 4, 5, 6, 7}, VIA enabled, USB on the right half. The wrap-around also happens without RGBLIGHT_LED_MAP, because the plain branch performs the same subtraction.
Link to product page (if applicable)
https://github.com/josefadamcik/SofleKeyboard
Operating System
Linux (Arch). Built with the official ghcr.io/qmk/qmk_cli Docker image, qmk_firmware master at 0.34.4. quantum/rgblight/rgblight.c is identical on develop.
qmk doctor Output
N/A: built with the official Docker image.
Is AutoHotKey / Karabiner installed
- AutoHotKey (Windows)
- Karabiner (macOS)
Other keyboard-related software installed
VIA (usevia.app).
Additional Context
I only verified this on RP2040. The wrap-around in rgblight_led_index() does not depend on the platform, so any set_color implementation without a bounds check is affected. What gets overwritten depends on what the linker places after the LED buffer.
Source: qmk/qmk_firmware