This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 5f51e95944 fix: ensure `BufferBuilder::truncate` doesn't overset
length (#9288)
5f51e95944 is described below
commit 5f51e95944b4ba88e0a7091079f807155d9fe698
Author: Jeffrey Vo <[email protected]>
AuthorDate: Fri Jan 30 01:16:38 2026 +0900
fix: ensure `BufferBuilder::truncate` doesn't overset length (#9288)
# Which issue does this PR close?
<!--
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 #9286
# Rationale for this change
<!--
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.
-->
Miri reports undefined behaviour because `truncate` didn't check new
length when setting the length for `BufferBuilder`, meaning when used in
conjunction with `as_slice_mut` the pointer could potentially be
dereferenced for memory beyond its allocation.
# What changes are included in this PR?
<!--
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.
-->
Ensure we bound length in BufferBuilder so truncating to a larger length
does not change the length.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. 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)?
-->
Added test.
# Are there any user-facing changes?
<!--
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.
-->
No.
---
arrow-buffer/src/builder/mod.rs | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/arrow-buffer/src/builder/mod.rs b/arrow-buffer/src/builder/mod.rs
index 1abb8018ce..be877e39dc 100644
--- a/arrow-buffer/src/builder/mod.rs
+++ b/arrow-buffer/src/builder/mod.rs
@@ -330,7 +330,7 @@ impl<T: ArrowNativeType> BufferBuilder<T> {
#[inline]
pub fn truncate(&mut self, len: usize) {
self.buffer.truncate(len * std::mem::size_of::<T>());
- self.len = len;
+ self.len = self.len.min(len);
}
/// # Safety
@@ -443,4 +443,16 @@ mod tests {
builder.extend([3, 4]);
assert_eq!(builder.len(), 4);
}
+
+ #[test]
+ fn truncate_safety() {
+ let mut builder = BufferBuilder::from(vec![40, -63, 90]);
+ assert_eq!(builder.len(), 3);
+ builder.truncate(151);
+ assert_eq!(builder.len(), 3);
+ builder.advance(219);
+ assert_eq!(builder.len(), 222);
+ let slice = builder.as_slice_mut();
+ assert_eq!(slice.len(), 222);
+ }
}