You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
d/p_realloc_malloc() can free an invalid pointer and cause crash (esp32 variants only) #6023
d_free(ptr); // free old buffer if realloc failed (or min heap was exceeded)
returnd_malloc(size); // fallback to malloc
}
If reallocation is successful but the remaining heap is too low, validateFreeHeap() returns nullptr. d_free(ptr) then tries to free the original pointer, which is already invalid after successful realloc().
The same problem exists in p_realloc_malloc().
The solution is to keep a copy of the pointer returned by heap_caps_realloc(), and then use it to decide if we need to free ptr: if (!newBuffer) d_free(ptr);.
@softhack007 Your proposed check fixes the low-heap double-free in d_realloc_malloc() for nonzero sizes. It needs one more case for size == 0.
If heap_caps_realloc() succeeds, it consumes the original allocation. If validateFreeHeap() then rejects the result, it frees the returned allocation. The wrapper must not free ptr again.
If heap_caps_realloc() fails for a nonzero size, the original allocation remains valid. The wrapper can free it before the fallback.
For size == 0, ESP-IDF frees ptr and returns null. The proposed null check alone would therefore free ptr twice. This behavior is present in ESP-IDF 5.5.4 and 4.4.8.
For the ESP32 implementation, this handles those cases:
The boolean records success before validation can free buffer.
p_realloc_malloc() does not have the same validation issue: it does not call validateFreeHeap(). It does have the zero-size double-free, so it also needs a zero-size guard before heap_caps_realloc().
I checked the code and ESP-IDF source; I did not run hardware tests. I recommend testing successful in-place and moved reallocations, heap-validation rejection, allocation failure, and zero size.
Learnings used
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:16:08.536Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Comments
Comments should have a space between the delimiting characters (e.g. `//`) and the comment text.
We're gradually adopting this style - don't worry if you see older code without spaces!
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:24:49.630Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Blocks
Whether the opening bracket of e.g. an `if` block is in the same line as the condition or in a separate line is up to your discretion. If there is only one statement, leaving out block brackets is acceptable.
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:16:08.536Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Blocks
There should always be a space between a keyword and its condition and between the condition and brace.
Within the condition, no space should be between the parenthesis and variables.
Spaces between variables and operators are up to the authors discretion.
There should be no space between function names and their argument parenthesis.
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:16:08.536Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Blocks
Prefer compact function parameter lists and `if` conditions rather than putting each parameter or condition part on its own line. Wrap parameter lists only when they become very long.
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:16:08.536Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
Don't stress too much about style! When in doubt, just match the style in the files you're editing. 😊
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:16:08.536Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Comments
There is no hard character limit for a comment within a line,
though as a rule of thumb consider wrapping after 120 characters.
Inline comments are OK if they describe that line only and are not exceedingly wide.
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:42:25.881Z
Learning: Applies to wled00/data/pixelforge/omggif.js : Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Indentation
We use tabs for indentation in Web files (.html/.css/.js) and spaces (2 per indentation level) for all other files.
You are all set if you have enabled `Editor: Detect Indentation` in VS Code.
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:42:25.881Z
Learning: Applies to wled00/data/index.css : Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Indentation
We use tabs for indentation in Web files (.html/.css/.js) and spaces (2 per indentation level) for all other files.
You are all set if you have enabled `Editor: Detect Indentation` in VS Code.
@coderabbitai do we need special handling for ptr == nullptr or is the suggested fix sufficient? Check against the behaviour of a plain heap_caps_realloc(...) call.
@softhack007 No additional ptr == nullptr branch is needed for memory safety in the fix from my previous reply.
If ptr == nullptr and size > 0, plain heap_caps_realloc() attempts a new allocation. The proposed wrapper does the same.
If that allocation fails, the wrapper calls d_free(nullptr), which is safe, then tries d_malloc(size). If validation rejects a successful allocation, it frees that allocation before the fallback.
If ptr == nullptr and size == 0, the guard calls d_free(nullptr) and returns nullptr. The checked ESP-IDF implementations also return nullptr for a plain call with these arguments.
The wrapper is not identical to a plain heap_caps_realloc() call. It can reject an allocation through validateFreeHeap() and try a fallback. For a non-null pointer and nonzero size, it also frees the original buffer on allocation failure; a plain call leaves that buffer allocated. An explicit null-pointer branch would only be needed if you want the initial allocation to use d_malloc() directly.
I checked the ESP-IDF 5.5.4 and 4.4.8 implementations. I did not run hardware tests.
Learnings used
Learnt from: CR
Repo: wled/WLED PR: 0
File: CONTRIBUTING.md:0-0
Timestamp: 2026-10-31T16:16:08.536Z
Learning: Applicability reference:
AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference
## Required User Agreement
### No agent-posted replies or issues
Reviewer time is limited, and checking long AI-generated explanations for reasoning errors and hallucinations is costly. Even after the user agreement is confirmed:
- Creating or updating pull requests remains allowed (after the user agreement is confirmed). Keep PR descriptions concise and factual, and include a summary written by the user in their own words, leading with what they want to achieve or what bothers them in plain language, as described in [CONTRIBUTING.md](CONTRIBUTING.md#opening-issues--feature-requests).
Source excerpt:
# Thank you for making WLED better!
## Coding Guidelines
### Code style
#### Comments
Comments should have a space between the delimiting characters (e.g. `//`) and the comment text.
We're gradually adopting this style - don't worry if you see older code without spaces!
Learnt from: DedeHai
Repo: wled/WLED PR: 4791
File: wled00/util.cpp:737-743
Timestamp: 2025-09-15T19:13:56.469Z
Learning: In WLED's util.cpp, the *_realloc_malloc functions (p_realloc_malloc and d_realloc_malloc) are intentionally designed to free the original buffer on realloc failure and allocate a new buffer, implementing a "replace buffer" semantic rather than traditional realloc behavior. This is documented in the function comments and is the intended design by the author DedeHai.
Learnt from: softhack007
Repo: wled/WLED PR: 0
File: :0-0
Timestamp: 2026-10-03T14:59:43.661Z
Learning: In wled/WLED, softhack007 prefers contributors to answer maintainer questions directly with short explanations in their own words, even if the explanations are technically imprecise or the English is imperfect. Contributors should state uncertainty and actual test results rather than relay unverified AI-generated technical answers. AI may assist with translation or wording, but should not manufacture explanations that shift fact-checking and AI-session supervision onto maintainers.
Activity
softhack007 commentedon Dec 2, 2026
@coderabbitai please check if my suggested fix is correct.
coderabbitai commentedon Dec 2, 2026
Analysis chain
Script executed:
Length of output: 26497
Script executed:
Length of output: 74638
Script executed:
Length of output: 21215
Script executed:
Length of output: 2218
@softhack007Your proposed check fixes the low-heap double-free ind_realloc_malloc()for nonzero sizes. It needs one more case forsize == 0.heap_caps_realloc()succeeds, it consumes the original allocation. IfvalidateFreeHeap()then rejects the result, it frees the returned allocation. The wrapper must not freeptragain.heap_caps_realloc()fails for a nonzero size, the original allocation remains valid. The wrapper can free it before the fallback.size == 0, ESP-IDF freesptrand returns null. The proposed null check alone would therefore freeptrtwice. This behavior is present in ESP-IDF 5.5.4 and 4.4.8.For the ESP32 implementation, this handles those cases:
The boolean records success before validation can free
buffer.p_realloc_malloc()does not have the same validation issue: it does not callvalidateFreeHeap(). It does have the zero-size double-free, so it also needs a zero-size guard beforeheap_caps_realloc().I checked the code and ESP-IDF source; I did not run hardware tests. I recommend testing successful in-place and moved reallocations, heap-validation rejection, allocation failure, and zero size.
Learnings used
You are interacting with an AI system.
fix double free error in p/d_realloc_malloc (fixes #6023)
softhack007 commentedon Dec 3, 2026
@coderabbitai do we need special handling for
ptr == nullptror is the suggested fix sufficient? Check against the behaviour of a plainheap_caps_realloc(...)call.coderabbitai commentedon Dec 3, 2026
Analysis chain
Script executed:
Length of output: 59593
Script executed:
Length of output: 14330
@softhack007No additionalptr == nullptrbranch is needed for memory safety in the fix from my previous reply.ptr == nullptrandsize > 0, plainheap_caps_realloc()attempts a new allocation. The proposed wrapper does the same.d_free(nullptr), which is safe, then triesd_malloc(size). If validation rejects a successful allocation, it frees that allocation before the fallback.ptr == nullptrandsize == 0, the guard callsd_free(nullptr)and returnsnullptr. The checked ESP-IDF implementations also returnnullptrfor a plain call with these arguments.The wrapper is not identical to a plain
heap_caps_realloc()call. It can reject an allocation throughvalidateFreeHeap()and try a fallback. For a non-null pointer and nonzero size, it also frees the original buffer on allocation failure; a plain call leaves that buffer allocated. An explicit null-pointer branch would only be needed if you want the initial allocation to used_malloc()directly.I checked the ESP-IDF 5.5.4 and 4.4.8 implementations. I did not run hardware tests.
Learnings used
You are interacting with an AI system.
coderabbitai commentedon Dec 3, 2026
fix double free error in p/d_realloc_malloc (fixes #6023) (#6024)
fix double free error in p/d_realloc_malloc (fixes wled#6023) (wled#6024