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 0d645410 fix(arrow/cdata): export an empty view sizes buffer as the 
zero region (#1544)
0d645410 is described below

commit 0d6454102392dcfc2a27fbb9a188c4a2c83aa500
Author: Harrison Crosse <[email protected]>
AuthorDate: Thu Nov 12 13:23:19 2026 -0500

    fix(arrow/cdata): export an empty view sizes buffer as the zero region 
(#1544)
    
    ### Rationale for this change
    
    A view array with no data buffers exports a `NULL` pointer for its
    variadic buffer sizes. The C data interface allows `NULL` for an empty
    buffer, but `exportArray` uses `kGoCdataZeroRegion` for other empty
    buffers to support consumers that do not import `NULL`
    (apache/arrow#33936).
    
    ### What changes are included in this PR?
    
    `exportArray` points an empty sizes buffer at `kGoCdataZeroRegion`, and
    `releaseExportedArray` skips freeing it. Freeing the zero region crashes
    the `string_view_inline` row and produces a `SEGV` in the `-asan` run.
    
    ### Are these changes tested?
    
    `TestExtensionArrayRoundTrip` now checks that the last exported buffer
    is not `NULL`. The `string_view_inline` row fails without this change
    and passes with it.
    
    With Go 1.26.10, `go test -race -tags assert,test ./arrow/cdata/...`,
    `go test -asan -short -tags assert,test ./arrow/cdata/...` on Linux,
    `golangci-lint` 2.13.2 and the Release Audit Tool passed.
    
    ### Are there any user-facing changes?
    
    Yes. Exported view arrays with no data buffers point their sizes buffer
    at the zero region instead of `NULL`. There is no API change.
---
 arrow/cdata/cdata_exports.go | 17 ++++++++++-------
 arrow/cdata/cdata_test.go    |  1 +
 arrow/cdata/exports.go       |  5 ++++-
 3 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/arrow/cdata/cdata_exports.go b/arrow/cdata/cdata_exports.go
index ef2c8b39..a760ec53 100644
--- a/arrow/cdata/cdata_exports.go
+++ b/arrow/cdata/cdata_exports.go
@@ -392,14 +392,17 @@ func exportArray(arr arrow.Array, out *CArrowArray, 
outSchema *CArrowSchema) {
                        cBufs[i] = (*C.void)(unsafe.Pointer(&buf.Bytes()[0]))
                }
 
-               // with no data buffers the sizes buffer would be empty, and 
the C data
-               // interface allows a NULL pointer for an empty buffer
-               if has_buffer_sizes_buffer && len(buffers) > 2 {
-                       sizes := allocateBufferSizeArr(len(buffers[2:]))
-                       for i, buf := range buffers[2:] {
-                               sizes[i] = C.int64_t(buf.Len())
+               if has_buffer_sizes_buffer {
+                       if len(buffers) > 2 {
+                               sizes := allocateBufferSizeArr(len(buffers[2:]))
+                               for i, buf := range buffers[2:] {
+                                       sizes[i] = C.int64_t(buf.Len())
+                               }
+                               cBufs[nbuffers-1] = 
(*C.void)(unsafe.Pointer(&sizes[0]))
+                       } else {
+                               // releaseExportedArray must not free this 
dummy buffer
+                               cBufs[nbuffers-1] = 
(*C.void)(unsafe.Pointer(&C.kGoCdataZeroRegion))
                        }
-                       cBufs[nbuffers-1] = (*C.void)(unsafe.Pointer(&sizes[0]))
                }
                out.buffers = (*unsafe.Pointer)(unsafe.Pointer(&cBufs[0]))
        }
diff --git a/arrow/cdata/cdata_test.go b/arrow/cdata/cdata_test.go
index 22ac6b2d..b9ee87c2 100644
--- a/arrow/cdata/cdata_test.go
+++ b/arrow/cdata/cdata_test.go
@@ -1226,6 +1226,7 @@ func TestExtensionArrayRoundTrip(t *testing.T) {
                        defer ReleaseCArrowArray(&out)
                        defer ReleaseCArrowSchema(&sc)
                        assert.EqualValues(t, tt.nbuffers, out.n_buffers)
+                       assert.NotNil(t, unsafe.Slice(out.buffers, 
out.n_buffers)[out.n_buffers-1])
 
                        field, imported, err := ImportCArray(&out, &sc)
                        require.NoError(t, err)
diff --git a/arrow/cdata/exports.go b/arrow/cdata/exports.go
index a3584046..1dd615dd 100644
--- a/arrow/cdata/exports.go
+++ b/arrow/cdata/exports.go
@@ -32,6 +32,7 @@ import (
 // #include "abi.h"
 // #include "helpers.h"
 //
+// extern const uint8_t kGoCdataZeroRegion[8];
 // typedef const char cchar_t;
 // extern int streamGetSchema(struct ArrowArrayStream*, struct ArrowSchema*);
 // extern int streamGetNext(struct ArrowArrayStream*, struct ArrowArray*);
@@ -131,7 +132,9 @@ func releaseExportedArray(arr *CArrowArray) {
 
        if arr.n_buffers > 0 {
                if internal.HasBufferSizesBuffer(data.DataType().ID()) {
-                       C.free(unsafe.Slice(arr.buffers, 
arr.n_buffers)[arr.n_buffers-1])
+                       if sizes := unsafe.Slice(arr.buffers, 
arr.n_buffers)[arr.n_buffers-1]; sizes != unsafe.Pointer(&C.kGoCdataZeroRegion) 
{
+                               C.free(sizes)
+                       }
                }
                C.free(unsafe.Pointer(arr.buffers))
        }

Reply via email to