[heap-data] use allocate-first pattern in UpdateBuffer() - #12794
Conversation
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>
Summary of ChangesHello, 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
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
Library files
|
|
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
left a comment
There was a problem hiding this comment.
@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.
Heap::Data::UpdateBuffer()
Heap::Data::UpdateBuffer()UpdateBuffer()
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
UpdateBuffer()frees the old buffer before allocating a new one. If the allocation fails,mDatastill holds the old (now freed) pointer, leaving the object in an inconsistent state.This changes
UpdateBuffer()to use the same allocate-first pattern used byHeap::String::Set: allocate first, free only on success. On allocation failure the existing buffer is preserved.