Skip to content

Stop terminal color codes from crashing boards without a suitable palette - #11705

Merged
dhalbert merged 1 commit into
adafruit:mainfrom
mikeysklar:fix-terminal-color-crash
Nov 1, 2026
Merged

dhalbert merged 1 commit into
adafruit:mainfrom
mikeysklar:fix-terminal-color-crash

Conversation

@mikeysklar

@mikeysklar mikeysklar commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

Stop a terminal color code from crashing the board when the terminal has no suitable palette.

Why

The terminal treats any color source as a palette, so a color code can corrupt memory or hard fault.

Found while testing #10478 and #11704.

import displayio, terminalio
fw, fh = terminalio.FONT.get_bounding_box()
g = displayio.TileGrid(terminalio.FONT.bitmap, pixel_shader=displayio.ColorConverter(),
                       width=10, height=2, tile_width=fw, tile_height=fh)
t = terminalio.Terminal(g, terminalio.FONT)
print("BEFORE")
t.write("\x1b[31mX")
print("AFTER")
Board Before After
CLUE nRF52840 Hard fault Prints AFTER, 3 of 3
Metro M4 AirLift Hard fault, 3 of 3 Prints AFTER, 3 of 3
Metro RP2040 Hard fault, 3 of 3 Prints AFTER, 3 of 3
Metro RP2350 Hard fault, 3 of 3 Prints AFTER, 3 of 3
Metro ESP32-S3 Hard fault, 3 of 3 Prints AFTER, 3 of 3
Feather nRF52840 Hard fault, 3 of 3 Prints AFTER, 3 of 3
Metro ESP32-S2 Hard fault, 3 of 3 Prints AFTER, 3 of 3
Feather STM32F405 Hard fault, 3 of 3 Prints AFTER, 3 of 3
Metro M0 Express No fault, 3 of 3 No fault, 3 of 3. Color codes are compiled out on SAMD21, so this code is not in the build
Desktop lab, x86-64, 10 runs per case Before After
ColorConverter, TilePaletteMapper, including a shader swapped in later Hard fault, 10 of 10 No fault
Palette(1) next to another object Hard fault or neighbor corrupted, 10 of 10 No fault, neighbor intact
Palette(2), Palette(8), REPL Colors change Same readback
All 1,351 runs 0 faults, 0 tracebacks

Behavior change: ESC[4xm on a Palette(1) used to set entry 0 and is now ignored.

Flash cost: 64 to 92 bytes on CLUE, Feather nRF52840, M4, RP2350, STM32F405 and ESP32-S2.

@lynt-smitka your cp-desktop-lab found this crash before any hardware run.

AI assistance

Claude Code wrote the fix and ran the tests above on boards and the lab. Before and after share a commit on every board.

🤖 Generated with Claude Code

The m handler treated any pixel shader as a Palette, so a color escape on
a ColorConverter, TilePaletteMapper or one-entry Palette could hard fault
or corrupt memory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dhalbert dhalbert left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@dhalbert
dhalbert merged commit a9bf6ec into adafruit:main Nov 1, 2026
630 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants