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 2a96f6b8 fix(arrow/cdata): reject malformed union imports (#1696)
2a96f6b8 is described below
commit 2a96f6b84fe9fadcf2ba0af669ea16b514863cf4
Author: Muhammad Umar Nadeem <[email protected]>
AuthorDate: Fri Nov 27 09:07:07 2026 -0800
fix(arrow/cdata): reject malformed union imports (#1696)
### Rationale for this change
Importing a union with duplicate type codes or a sparse child shorter
than the parent's offset and length panics instead of returning an
error.
Fixes #1679.
### What changes are included in this PR?
Reject duplicate type codes before constructing the union type, and
check sparse-child coverage before constructing the array data. Both
cases return `arrow.ErrInvalid`.
### Are these changes tested?
Added regressions for duplicate sparse/dense type codes and short sparse
children, including sliced parents and release-count checks. Both
malformed-input cases panic before the fix.
The C Data and array package tests passed with `assert,test` and
`assert,test,noasm`; the C Data race check passed too. Default `go vet`
reports the same two existing `unsafe.Pointer` warnings as upstream in
`interface.go`; the remaining analyzers passed.
### Are there any user-facing changes?
Malformed union imports return errors instead of panicking.
---
arrow/cdata/cdata.go | 13 +++++++++
arrow/cdata/cdata_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+)
diff --git a/arrow/cdata/cdata.go b/arrow/cdata/cdata.go
index b62cb012..9cba226e 100644
--- a/arrow/cdata/cdata.go
+++ b/arrow/cdata/cdata.go
@@ -381,6 +381,7 @@ func importSchema(schema *CArrowSchema) (ret arrow.Field,
err error) {
return ret, fmt.Errorf("invalid union type code
spec %q", f)
}
var typeCodes []arrow.UnionTypeCode
+ var seenCodes [128]bool
for i := range strings.SplitSeq(val, ",") {
v, e := strconv.ParseInt(i, 10, 8)
if e != nil {
@@ -391,6 +392,10 @@ func importSchema(schema *CArrowSchema) (ret arrow.Field,
err error) {
err = fmt.Errorf("%w: negative type
code in union: format string %s", arrow.ErrInvalid, f)
return
}
+ if seenCodes[v] {
+ return ret, fmt.Errorf("%w: duplicate
union type code %d", arrow.ErrInvalid, v)
+ }
+ seenCodes[v] = true
typeCodes = append(typeCodes,
arrow.UnionTypeCode(v))
}
@@ -842,6 +847,14 @@ func (imp *cimporter) doImport() error {
if err := imp.checkNoNulls(); err != nil {
return err
}
+ // validateCArrayHeader checked that offset + length fits in an
int.
+ end := int(imp.arr.offset) + int(imp.arr.length)
+ for i, child := range imp.children {
+ if child.data.Len() < end {
+ return fmt.Errorf("%w: sparse union child %d
has length %d, expected at least %d",
+ arrow.ErrInvalid, i, child.data.Len(),
end)
+ }
+ }
var buf *memory.Buffer
var err error
diff --git a/arrow/cdata/cdata_test.go b/arrow/cdata/cdata_test.go
index cab4cac7..772e9587 100644
--- a/arrow/cdata/cdata_test.go
+++ b/arrow/cdata/cdata_test.go
@@ -1298,6 +1298,77 @@ func TestEmptyUnionExport(t *testing.T) {
assert.NotEqualValues(t, unsafe.Pointer(nil), buffers[0])
}
+func TestImportUnionDuplicateTypeCodes(t *testing.T) {
+ for _, format := range []string{"+us:0,0", "+ud:0,0"} {
+ t.Run(format, func(t *testing.T) {
+ schemas := testUnion([]string{format, "i", "C"},
[]string{"", "i", "u"}, []int64{0, 0, 0})
+ defer freeMallocedSchemas(schemas)
+ schema := unsafe.Slice(schemas, 3)[0]
+ defer ReleaseCArrowSchema(schema)
+ arr := createTestSparseUnion()
+ defer arr.Release()
+ var out CArrowArray
+ ExportArrowArray(arr, &out, nil)
+ count := wrapCountingRelease(&out)
+ defer freeCountingRelease(count)
+ defer ReleaseCArrowArray(&out)
+
+ _, imported, err := ImportCArray(&out, schema)
+ require.ErrorIs(t, err, arrow.ErrInvalid)
+ require.Nil(t, imported)
+ assert.True(t, schemaIsReleased(schema))
+ assert.Zero(t, getCountingReleaseCount(count), "schema
errors leave the array with the caller")
+ ReleaseCArrowArray(&out)
+ assert.Equal(t, 1, getCountingReleaseCount(count))
+ })
+ }
+}
+
+func TestImportSparseUnionChildCoverage(t *testing.T) {
+ for _, sliced := range []bool{false, true} {
+ for _, short := range []bool{false, true} {
+ t.Run(fmt.Sprintf("sliced=%t/short=%t", sliced, short),
func(t *testing.T) {
+ arr := createTestSparseUnion()
+ defer arr.Release()
+ var out CArrowArray
+ var schema CArrowSchema
+ ExportArrowArray(arr, &out, &schema)
+ defer ReleaseCArrowSchema(&schema)
+ count := wrapCountingRelease(&out)
+ defer freeCountingRelease(count)
+ defer ReleaseCArrowArray(&out)
+ start := int64(0)
+ if sliced {
+ out.offset, out.length = 1, 3
+ start = 1
+ }
+ if short {
+ child := unsafe.Slice(out.children,
out.n_children)[0]
+ child.length = out.length - 1
+ if sliced {
+ child.length = out.length //
enough rows without the parent's offset
+ }
+ }
+
+ _, imported, err := ImportCArray(&out, &schema)
+ assert.True(t, schemaIsReleased(&schema))
+ if short {
+ require.ErrorIs(t, err,
arrow.ErrInvalid)
+ require.Nil(t, imported)
+ } else {
+ require.NoError(t, err)
+ expected := array.NewSlice(arr, start,
int64(arr.Len()))
+ assert.True(t, array.Equal(expected,
imported))
+ expected.Release()
+ imported.Release()
+ }
+ ReleaseCArrowArray(&out)
+ assert.Equal(t, 1,
getCountingReleaseCount(count))
+ })
+ }
+ }
+}
+
func TestExtensionArrayRoundTrip(t *testing.T) {
tests := []struct {
name string