Repository navigation
[59_maintenance] fix(arrow-buffer): prevent use-after-free in Buffer::shrink_to_fit when MemoryReservation::resize panics - #11787
Merged
Conversation
…en MemoryReservation::resize panics (apache#10932) <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. --> - Closes apache#10379. Buffer maintains two pointers that must always agree: `Buffer::ptr` (the raw *const u8 into the allocation) and `Bytes::ptr` (the NonNull<u8> that owns the allocation). Before this fix, `shrink_to_fit` only updated `Buffer::ptr` if `try_realloc` returned Ok. But `try_realloc` can panic; specifically inside `MemoryReservation::resize`, a user-supplied callback after the realloc syscall already succeeded and `Bytes::ptr` was updated. That left the two pointers out of sync: `Bytes::ptr` pointed to the new (valid) allocation, `Buffer::ptr` still pointed to the old (freed) allocation. Any caller that used `catch_unwind` to recover from the panic could then call `buf.as_slice()` and read freed memory. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> replaced the conditional if try_realloc(...).is_ok() { update ptr } with a PtrSync drop guard that unconditionally syncs `Buffer::ptr` to `Bytes::ptr` + offset when it drops. Since Rust runs destructors during panic unwinding, the guard fires before the panic propagates. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> yes. a similar test to what was used in apache#10379 <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 3. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. --> no <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please call them out. --> (cherry picked from commit 9a8400e)
4 of 5 tasks
Jefffrey
approved these changes
Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
59.3.2(October 2026) #11664Cherry-pick of 9a8400e
Conflict resolved by hand in
arrow-buffer/src/buffer/immutable.rs: kept the nestedif letform instead of let-chains (59's MSRV is 1.85).bytes.rsapplied cleanly.