This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 39d13259 fix(arrow/memory): avoid retaining parent for invalid slices
(#1569)
39d13259 is described below
commit 39d1325988168f012e06d1361d7d27f8c639b211
Author: Ayaan <[email protected]>
AuthorDate: Mon Nov 16 23:43:24 2026 +0530
fix(arrow/memory): avoid retaining parent for invalid slices (#1569)
### Rationale for this change
Part 1 of #77: an invalid buffer slice panics after retaining its
parent, leaving an allocation outstanding.
### What changes are included in this PR?
Evaluate the slice before retaining the parent. Further IPC loader
cleanup remains for later work.
### Are these changes tested?
A checked-allocator test fails on main and passes with this change. IPC
tests, race tests, and vet pass. The local stream corpus has 3 leaking
inputs after this change, down from 35.
### Are there any user-facing changes?
Invalid slices still panic but no longer retain the parent buffer.
---
arrow/memory/buffer.go | 3 ++-
arrow/memory/buffer_test.go | 11 +++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/arrow/memory/buffer.go b/arrow/memory/buffer.go
index 8893d1b5..8b3d43ca 100644
--- a/arrow/memory/buffer.go
+++ b/arrow/memory/buffer.go
@@ -60,8 +60,9 @@ func NewResizableBuffer(mem Allocator) *Buffer {
}
func SliceBuffer(buf *Buffer, offset, length int) *Buffer {
+ data := buf.Bytes()[offset : offset+length]
buf.Retain()
- b := &Buffer{parent: buf, buf: buf.Bytes()[offset : offset+length],
length: length}
+ b := &Buffer{parent: buf, buf: data, length: length}
b.refCount.Add(1)
return b
}
diff --git a/arrow/memory/buffer_test.go b/arrow/memory/buffer_test.go
index 7b413ec1..1e0479f1 100644
--- a/arrow/memory/buffer_test.go
+++ b/arrow/memory/buffer_test.go
@@ -115,6 +115,17 @@ func TestBufferSlice(t *testing.T) {
slice.Release()
}
+func TestBufferSliceInvalidRangeDoesNotLeak(t *testing.T) {
+ mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+ defer mem.AssertSize(t, 0)
+
+ buf := memory.NewResizableBuffer(mem)
+ buf.Resize(64)
+ defer buf.Release()
+
+ assert.Panics(t, func() { memory.SliceBuffer(buf, -1, 1) })
+}
+
func TestBufferResetFromOwnedSlice(t *testing.T) {
mem := memory.NewCheckedAllocator(&movingAllocator{})
defer mem.AssertSize(t, 0)