Skip to content

fix double free error in p/d_realloc_malloc (fixes #6023) - #6024

Merged
softhack007 merged 2 commits into
mainfrom
fix_6023
Dec 3, 2026
Merged

softhack007 merged 2 commits into
mainfrom
fix_6023

Conversation

@softhack007

@softhack007 softhack007 commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

What it does

Solves a free-after-free error in our memory reallocation functions (see #6023 for details).
If realloc() succeeds but validateFreeHeap() rejects due to low memory, the function called free(ptr) on an invalid pointer. This can lead to crashes or later heap corruption abort.

  • handles size == 0 separately (For size == 0, ESP-IDF frees ptr and returns null)
  • only frees original buffer if realloc has failed (successful realloc() frees the old ptr)

AI assistance: no

Not while my own bio brains are still functioning ;-D

Checklist

  • I have personally reviewed all changes (incl. source code) before opening this PR.
  • I'm ready to explain my proposed source code changes in detail (without an AI agent answering for me).
  • I agree to follow this project's Code of Conduct.

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory handling when resizing allocations, including more reliable fallback behavior when a resize cannot be completed.
    • Zero-size allocation requests now release the associated memory and return no allocation. This helps prevent unexpected memory retention and keeps allocation behavior consistent across supported platforms.

* handle size == 0 separately (For size == 0, ESP-IDF frees ptr and returns null)
* only free original buffer if realloc has failed (successful realloc() frees the old ptr)
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

The realloc helpers now free the supplied pointer and return nullptr for zero-size requests. The ESP32 helper distinguishes realloc failure from free-heap validation failure before falling back to d_malloc.

Changes

Allocator Reallocation Handling

Layer / File(s) Summary
Zero-size and realloc failure handling
wled00/util.cpp
Both helpers free the supplied pointer and return nullptr for zero-size requests. For nonzero ESP32 requests, the original pointer is freed before fallback allocation only if heap_caps_realloc fails. If free-heap validation rejects the result, the original pointer remains allocated before fallback.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Possibly related issues

  • Issue 6023 describes the double-free behavior after successful realloc() and names both helpers changed in this pull request.

Suggested labels: bug

Suggested reviewers: dedehai

Merge Risk: Minimal · up to 3584d

Merge Risk: ⚪ Minimal · up to 3584d

This change fixes a possible double free in the realloc helpers on low-memory ESP32 devices. No remaining merge-blocking risk is evident from the supplied review.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the specific bug fixed: a double free in the p_realloc_malloc and d_realloc_malloc functions, with the issue reference (#6023). The changeset matches this. It frees the original pointe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the specific bug fixed: a double free in the p_realloc_malloc and d_realloc_malloc functions, with the issue reference (#6023). The changeset matches this. It frees the original pointer only when realloc fails, handles zero-size requests by freeing the pointer and returning nullptr, and keeps the original pointer allocated when validateFreeHeap rejects a successful realloc. The title is specific and on topic, so the check passes.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@softhack007
softhack007 requested a review from DedeHai December 3, 2026 00:27
@softhack007 softhack007 added this to the 16.1 milestone Dec 3, 2026
@softhack007 softhack007 added the bug label Dec 3, 2026
@softhack007 softhack007 self-assigned this Dec 3, 2026

@DedeHai DedeHai 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.

good find

@softhack007
softhack007 merged commit bee516b into main Dec 3, 2026
66 checks passed
@softhack007
softhack007 deleted the fix_6023 branch December 3, 2026 11:45
softhack007 added a commit that referenced this pull request Dec 3, 2026
Solves a free-after-free error in our memory reallocation functions (see #6023 for details).
If realloc() succeeds but validateFreeHeap() rejects due to low memory, the function called free(ptr) on an invalid pointer. This can lead to crashes or later heap corruption abort.

* handles size == 0 separately (For size == 0, ESP-IDF frees ptr and returns null)
* only frees original buffer if realloc has failed (successful realloc() frees the old ptr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

d/p_realloc_malloc() can free an invalid pointer and cause crash (esp32 variants only)

2 participants