Skip to content

[heap-data] use allocate-first pattern in UpdateBuffer() - #12794

Merged
jwhui merged 1 commit into
openthread:mainfrom
Oblivionsage:fix/heap-data-double-free
Apr 3, 2026
Merged

[heap-data] use allocate-first pattern in UpdateBuffer()#12794
jwhui merged 1 commit into
openthread:mainfrom
Oblivionsage:fix/heap-data-double-free

Conversation

@Oblivionsage

Copy link
Copy Markdown
Contributor

UpdateBuffer() frees the old buffer before allocating a new one. If the allocation fails, mData still holds the old (now freed) pointer, leaving the object in an inconsistent state.

This changes UpdateBuffer() to use the same allocate-first pattern used by Heap::String::Set: allocate first, free only on success. On allocation failure the existing buffer is preserved.

Heap::Data::UpdateBuffer() freed the existing buffer before
attempting to allocate a new one. If the allocation failed,
mData retained a dangling pointer to the already-freed buffer.
A subsequent Free() call (from the destructor or an error path)
would then free the same pointer again, causing a double-free.

This changes UpdateBuffer() to use the allocate-first pattern
(consistent with Heap::String::Set): the new buffer is allocated
first, and the old buffer is freed only after a successful
allocation. On allocation failure, the old buffer is preserved
and no dangling pointer is created.

Signed-off-by: Oblivionsage <cookieandcream560@gmail.com>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a potential memory management issue in the Heap::Data::UpdateBuffer method. By reordering the allocation and deallocation logic, the change ensures that the object maintains a valid state even when memory allocation fails, aligning the implementation with established patterns used elsewhere in the codebase.

Highlights

  • Memory Safety Improvement: Updated the UpdateBuffer method to adopt an allocate-first pattern, ensuring the existing buffer is only freed after a successful new allocation.
  • State Consistency: Prevented potential inconsistent object states by ensuring the original buffer remains intact if a memory allocation fails.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request modifies UpdateBuffer in heap_data.cpp to adopt an allocate-first pattern, ensuring the old buffer is preserved if a new allocation fails. The review feedback suggests simplifying the logic to reduce duplication and improve clarity by separating the allocation step from the buffer initialization.

Comment thread src/core/common/heap_data.cpp
@github-actions

Copy link
Copy Markdown

Merging #12794 into main

name branch text data bss total
ot-cli-ftd dd855c3 482784 860 66540 550184
f893ad7 482784 860 66540 550184
+/- +0 +0 +0 +0
ot-ncp-ftd dd855c3 448692 764 61776 511232
f893ad7 448692 764 61776 511232
+/- +0 +0 +0 +0
ot-cli-mtd dd855c3 374944 764 50972 426680
f893ad7 374944 764 50972 426680
+/- +0 +0 +0 +0
ot-ncp-mtd dd855c3 356076 764 46240 403080
f893ad7 356076 764 46240 403080
+/- +0 +0 +0 +0
ot-cli-ftd-br dd855c3 599160 864 136612 736636
f893ad7 599176 864 136612 736652
+/- +16 +0 +0 +16
ot-rcp dd855c3 63328 568 20804 84700
f893ad7 63328 568 20804 84700
+/- +0 +0 +0 +0
Library files
name branch text data bss total
libopenthread-ftd.a dd855c3 250481 95 40319 290895
f893ad7 250489 95 40319 290903
+/- +8 +0 +0 +8
libopenthread-cli-ftd.a dd855c3 61750 0 8091 69841
f893ad7 61750 0 8091 69841
+/- +0 +0 +0 +0
libopenthread-ncp-ftd.a dd855c3 33583 0 5948 39531
f893ad7 33583 0 5948 39531
+/- +0 +0 +0 +0
libopenthread-mtd.a dd855c3 168856 0 24783 193639
f893ad7 168864 0 24783 193647
+/- +8 +0 +0 +8
libopenthread-cli-mtd.a dd855c3 41459 0 8059 49518
f893ad7 41459 0 8059 49518
+/- +0 +0 +0 +0
libopenthread-ncp-mtd.a dd855c3 25807 0 5948 31755
f893ad7 25807 0 5948 31755
+/- +0 +0 +0 +0
libopenthread-ftd-br.a dd855c3 367628 99 110351 478078
f893ad7 367636 99 110351 478086
+/- +8 +0 +0 +8
libopenthread-cli-ftd-br.a dd855c3 80957 0 8131 89088
f893ad7 80957 0 8131 89088
+/- +0 +0 +0 +0
libopenthread-rcp.a dd855c3 9946 0 5060 15006
f893ad7 9946 0 5060 15006
+/- +0 +0 +0 +0
libopenthread-radio.a dd855c3 19725 0 238 19963
f893ad7 19725 0 238 19963
+/- +0 +0 +0 +0

@Oblivionsage

Copy link
Copy Markdown
Contributor Author

I'll keep the current approach as it stays consistent with the existing pattern in Heap::String::Set and keeps the zero-length case explicit. Happy to adjust if a maintainer prefers the suggested refactor

@abtink abtink left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Oblivionsage, thanks for submitting this. It is a good find indeed.
LGTM. Regarding the Gemini suggestion, I think the way you have it is better (smaller). Thanks again.

@abtink abtink changed the title fix: use allocate-first pattern in Heap::Data::UpdateBuffer [heap-data]use allocate-first pattern in Heap::Data::UpdateBuffer() Apr 1, 2026
@abtink abtink changed the title [heap-data]use allocate-first pattern in Heap::Data::UpdateBuffer() [heap-data] use allocate-first pattern in UpdateBuffer() Apr 1, 2026
@codecov

codecov Bot commented Apr 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.93%. Comparing base (dd855c3) to head (0bebfed).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12794      +/-   ##
==========================================
- Coverage   73.40%   70.93%   -2.48%     
==========================================
  Files         696      696              
  Lines       98509    93382    -5127     
==========================================
- Hits        72310    66237    -6073     
- Misses      26199    27145     +946     
Files with missing lines Coverage Δ
src/core/common/heap_data.cpp 91.48% <100.00%> (+0.18%) ⬆️

... and 307 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jwhui jwhui left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! 👍🏼

@jwhui
jwhui merged commit c7b922c into openthread:main Apr 3, 2026
134 of 138 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.

3 participants