Skip to content

fix: heap-allocate tinfl_decompressor to prevent stack overflow in renderDeflatedJson - #11

Merged
martinberlin merged 5 commits into
mainfrom
copilot/implement-payload-decompression-miniz
May 29, 2026
Merged

martinberlin merged 5 commits into
mainfrom
copilot/implement-payload-decompression-miniz

Conversation

Copilot AI commented May 29, 2026

Copy link
Copy Markdown
Contributor

tinfl_decompress_mem_to_heap places a tinfl_decompressor struct (~34 KB) on the task stack. On constrained ESP32 tasks (e.g. a BLE receive task), this immediately overflows the stack, causing a Stack protection fault panic at the first call to renderDeflatedJson.

Changes

  • src/FastJsonDL.cpp — Replaced the tinfl_decompress_mem_to_heap call with an inline decompression loop that:
    • mallocs tinfl_decompressor on the heap instead of the stack
    • Grows the output buffer via realloc in the same pattern as the original helper
    • Frees both allocations on all exit paths (success and error)
// Before — tinfl_decompressor lives on the task stack (~34 KB)
void* outBuf = tinfl_decompress_mem_to_heap(compressedData, compressedLen, &outLen, 0);

// After — decompressor state on the heap; same loop semantics
tinfl_decompressor* decomp = static_cast<tinfl_decompressor*>(malloc(sizeof(tinfl_decompressor)));
tinfl_init(decomp);
// ... incremental tinfl_decompress loop with realloc'd output buffer ...
free(decomp);

Stack usage of renderDeflatedJson drops from ~34 KB to a handful of pointer-sized locals.

Copilot AI changed the title [WIP] Implement payload decompression using miniz Add DEFLATE payload decompression (type 0x0002) and fix portrait-mode coordinate space May 29, 2026
Copilot AI requested a review from martinberlin May 29, 2026 16:17
Copilot AI changed the title Add DEFLATE payload decompression (type 0x0002) and fix portrait-mode coordinate space fix: heap-allocate tinfl_decompressor to prevent stack overflow in renderDeflatedJson May 29, 2026

@martinberlin martinberlin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Great implementation. Works as expected!
Added my own fork of Miniz since it was not prepared to be added as a component in ESP-IDF

@martinberlin
martinberlin marked this pull request as ready for review May 29, 2026 20:52
@martinberlin
martinberlin merged commit 80a40c7 into main May 29, 2026
1 check 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.

Implement payload decompression using miniz

2 participants