-
Notifications
You must be signed in to change notification settings - Fork 135
fix(array): validate binary_view and string_view payloads #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zeroshade
merged 3 commits into
apache:main
from
fallintoplace:fix/validate-binary-string-view
Jul 24, 2026
+317
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import ( | |
|
|
||
| "github.com/apache/arrow-go/v18/arrow" | ||
| "github.com/apache/arrow-go/v18/arrow/bitutil" | ||
| "github.com/apache/arrow-go/v18/arrow/endian" | ||
| "github.com/apache/arrow-go/v18/arrow/memory" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
@@ -77,6 +78,42 @@ func makeInt32ArrayRaw(t *testing.T, values []int32, validity []byte, nulls, len | |
| return arr | ||
| } | ||
|
|
||
| func makeBinaryViewArrayRaw(t *testing.T, headerBytes []byte, dataBuffers []*memory.Buffer, validity []byte, nulls, length, offset int) *BinaryView { | ||
| t.Helper() | ||
| var validityBuf *memory.Buffer | ||
| if validity != nil { | ||
| validityBuf = memory.NewBufferBytes(validity) | ||
| } | ||
| viewBuf := memory.NewBufferBytes(headerBytes) | ||
| buffers := append([]*memory.Buffer{validityBuf, viewBuf}, dataBuffers...) | ||
| data := NewData(arrow.BinaryTypes.BinaryView, length, buffers, nil, nulls, offset) | ||
| arr := NewBinaryViewData(data) | ||
| data.Release() | ||
| return arr | ||
| } | ||
|
|
||
| func makeStringViewArrayRaw(t *testing.T, headerBytes []byte, dataBuffers []*memory.Buffer, validity []byte, nulls, length, offset int) *StringView { | ||
| t.Helper() | ||
| var validityBuf *memory.Buffer | ||
| if validity != nil { | ||
| validityBuf = memory.NewBufferBytes(validity) | ||
| } | ||
| viewBuf := memory.NewBufferBytes(headerBytes) | ||
| buffers := append([]*memory.Buffer{validityBuf, viewBuf}, dataBuffers...) | ||
| data := NewData(arrow.BinaryTypes.StringView, length, buffers, nil, nulls, offset) | ||
| arr := NewStringViewData(data) | ||
| data.Release() | ||
| return arr | ||
| } | ||
|
|
||
| func setViewHeaderBufferIndex(raw []byte, idx int32) { | ||
| endian.Native.PutUint32(raw[8:12], uint32(idx)) | ||
| } | ||
|
|
||
| func setViewHeaderOffset(raw []byte, offset int32) { | ||
| endian.Native.PutUint32(raw[12:16], uint32(offset)) | ||
| } | ||
|
|
||
| func TestBinaryValidate(t *testing.T) { | ||
| t.Run("valid array passes", func(t *testing.T) { | ||
| // offsets [0,3,6,9], data "abcdefghi" — 3 elements of 3 bytes each | ||
|
|
@@ -201,6 +238,176 @@ func TestLargeStringValidate(t *testing.T) { | |
| }) | ||
| } | ||
|
|
||
| func TestBinaryViewValidate(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice regression set. A few high-value cases are missing — the first would have caught the empty-array panic in
|
||
| t.Run("empty arrays pass top level validation", func(t *testing.T) { | ||
| mem := memory.NewCheckedAllocator(memory.DefaultAllocator) | ||
| defer mem.AssertSize(t, 0) | ||
|
|
||
| binaryArr := NewBinaryViewBuilder(mem).NewBinaryViewArray() | ||
| defer binaryArr.Release() | ||
| stringArr := NewStringViewBuilder(mem).NewStringViewArray() | ||
| defer stringArr.Release() | ||
|
|
||
| assert.NoError(t, Validate(binaryArr)) | ||
| assert.NoError(t, ValidateFull(binaryArr)) | ||
| assert.NoError(t, Validate(stringArr)) | ||
| assert.NoError(t, ValidateFull(stringArr)) | ||
| }) | ||
|
|
||
| t.Run("valid array passes", func(t *testing.T) { | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes([]byte("hello")) | ||
| arr := makeBinaryViewArrayRaw(t, arrow.ViewHeaderTraits.CastToBytes(headers[:]), nil, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, arr.Validate()) | ||
| assert.NoError(t, arr.ValidateFull()) | ||
| }) | ||
|
|
||
| t.Run("out of line values pass top level validation", func(t *testing.T) { | ||
| value := []byte("this payload is out of line") | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes(value) | ||
| headers[0].SetIndexOffset(0, 0) | ||
| dataBuf := memory.NewBufferBytes(value) | ||
| arr := makeBinaryViewArrayRaw(t, arrow.ViewHeaderTraits.CastToBytes(headers[:]), []*memory.Buffer{dataBuf}, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, Validate(arr)) | ||
| assert.NoError(t, ValidateFull(arr)) | ||
| }) | ||
|
|
||
| t.Run("offset arrays use the correct view slot", func(t *testing.T) { | ||
| headers := [2]arrow.ViewHeader{} | ||
| headers[0].SetBytes([]byte("skip")) | ||
| headers[1].SetBytes([]byte("keep")) | ||
| arr := makeBinaryViewArrayRaw(t, arrow.ViewHeaderTraits.CastToBytes(headers[:]), nil, nil, 0, 1, 1) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, Validate(arr)) | ||
| assert.NoError(t, ValidateFull(arr)) | ||
| }) | ||
|
|
||
| t.Run("negative size passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| headerBytes := make([]byte, arrow.ViewHeaderSizeBytes) | ||
| endian.Native.PutUint32(headerBytes[:4], ^uint32(0)) | ||
| arr := makeBinaryViewArrayRaw(t, headerBytes, nil, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, arr.Validate()) | ||
| err := arr.ValidateFull() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "negative size") | ||
| }) | ||
|
|
||
| t.Run("missing referenced buffer passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes([]byte("this is longer than twelve")) | ||
| headers[0].SetIndexOffset(0, 0) | ||
| arr := makeBinaryViewArrayRaw(t, arrow.ViewHeaderTraits.CastToBytes(headers[:]), nil, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, arr.Validate()) | ||
| err := arr.ValidateFull() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "references buffer 0") | ||
| }) | ||
|
|
||
| t.Run("prefix mismatch passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| value := []byte("this payload is out of line") | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes(value) | ||
| headers[0].SetIndexOffset(0, 0) | ||
| headerBytes := append([]byte(nil), arrow.ViewHeaderTraits.CastToBytes(headers[:])...) | ||
| headerBytes[4] ^= 0xff | ||
| dataBuf := memory.NewBufferBytes(value) | ||
| arr := makeBinaryViewArrayRaw(t, headerBytes, []*memory.Buffer{dataBuf}, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, Validate(arr)) | ||
| err := ValidateFull(arr) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "out-of-line data begins with") | ||
| }) | ||
|
|
||
| t.Run("negative buffer offset passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| value := []byte("this payload is out of line") | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes(value) | ||
| headers[0].SetIndexOffset(0, 0) | ||
| headerBytes := append([]byte(nil), arrow.ViewHeaderTraits.CastToBytes(headers[:])...) | ||
| setViewHeaderOffset(headerBytes, -1) | ||
| dataBuf := memory.NewBufferBytes(value) | ||
| arr := makeBinaryViewArrayRaw(t, headerBytes, []*memory.Buffer{dataBuf}, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, Validate(arr)) | ||
| err := ValidateFull(arr) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "negative offset") | ||
| }) | ||
|
|
||
| t.Run("negative buffer index passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| value := []byte("this payload is out of line") | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes(value) | ||
| headers[0].SetIndexOffset(0, 0) | ||
| headerBytes := append([]byte(nil), arrow.ViewHeaderTraits.CastToBytes(headers[:])...) | ||
| setViewHeaderBufferIndex(headerBytes, -1) | ||
| dataBuf := memory.NewBufferBytes(value) | ||
| arr := makeBinaryViewArrayRaw(t, headerBytes, []*memory.Buffer{dataBuf}, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, Validate(arr)) | ||
| err := ValidateFull(arr) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "negative buffer index") | ||
| }) | ||
|
|
||
| t.Run("referenced range beyond buffer length passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| value := []byte("this payload is out of line") | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes(value) | ||
| headers[0].SetIndexOffset(0, 2) | ||
| dataBuf := memory.NewBufferBytes(value) | ||
| arr := makeBinaryViewArrayRaw(t, arrow.ViewHeaderTraits.CastToBytes(headers[:]), []*memory.Buffer{dataBuf}, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, Validate(arr)) | ||
| err := ValidateFull(arr) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "references range") | ||
| }) | ||
|
|
||
| t.Run("inline padding bytes fail ValidateFull", func(t *testing.T) { | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes([]byte("x")) | ||
| headerBytes := append([]byte(nil), arrow.ViewHeaderTraits.CastToBytes(headers[:])...) | ||
| headerBytes[8] = 1 | ||
| arr := makeBinaryViewArrayRaw(t, headerBytes, nil, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, arr.Validate()) | ||
| err := arr.ValidateFull() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "padding bytes were not all zero") | ||
| }) | ||
| } | ||
|
|
||
| func TestStringViewValidate(t *testing.T) { | ||
| t.Run("invalid utf8 passes Validate but fails ValidateFull", func(t *testing.T) { | ||
| var headers [1]arrow.ViewHeader | ||
| headers[0].SetBytes([]byte{0xff}) | ||
| arr := makeStringViewArrayRaw(t, arrow.ViewHeaderTraits.CastToBytes(headers[:]), nil, nil, 0, 1, 0) | ||
| defer arr.Release() | ||
|
|
||
| assert.NoError(t, arr.Validate()) | ||
| err := arr.ValidateFull() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "not valid utf8") | ||
| }) | ||
| } | ||
|
|
||
| func TestTopLevelValidate(t *testing.T) { | ||
| t.Run("Validate dispatches to Validator", func(t *testing.T) { | ||
| // non-monotonic string array: passes setData but ValidateFull must fail | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking:
ValidateFullpanics on empty (length-0) view arrays.Validate()(viavalidateViewLayout) returns early whenlength == 0and never dereferencesbuffers[1], but for an empty view arraybuffers[1]isnil, so this hoisteddata.buffers[1].Bytes()panics.Reproduced on the standard builder output (and on hand-built data):
This regressed in the "tighten view validation helpers" commit — patch 1 only touched
buffers[1]inside the loop, so empty arrays were safe. It also undercuts the helper's intent: validation should report malformed data, not crash on a valid empty array.