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 b97dc35e fix(arrow/ipc): reject out-of-range decimal precision in
schemas (#1681)
b97dc35e is described below
commit b97dc35e89283e32c656c98ac3373c2d4b6652f4
Author: Ayaan <[email protected]>
AuthorDate: Thu Nov 26 00:13:16 2026 +0530
fix(arrow/ipc): reject out-of-range decimal precision in schemas (#1681)
### Rationale for this change
Part of #1676. `decimalFromFB` built decimal types directly from the
flatbuffer fields, so an IPC schema with decimal precision 0 (or above
the maximum for its width) was accepted. Arrow C++ rejects these. A
corpus input from arrow-testing
(`crash-5e88bae6ac5250714e8c8bc73b9d67b949fadbb4`) hits this case.
### What changes are included in this PR?
- `decimalFromFB` now builds types with `arrow.NewDecimalType`, which
validates precision per width: 1-9 (decimal32), 1-18 (decimal64), 1-38
(decimal128), 1-76 (decimal256). These match C++. The error propagates
as a schema read error. Scale is not checked, as before.
- `TestDecimalFromFBPrecision`: each width at precision -1, 0, 1, the
maximum and maximum+1, plus width and negative scale preservation.
- The `crash-5e88...` input now errors, so it is removed from the
`clean` map in `TestStreamFuzzRegression`.
The other half of #1676, structural flatbuffer verification, is not
addressed here. Go flatbuffers v25.12.19 and the generated `flatbuf`
code have no verifier API. #1676 stays open for it.
### Are these changes tested?
Yes. `go test -tags assert,test -race ./arrow/ipc/...` passes, as does
the run without tags. `golangci-lint` reports 0 issues on the changed
files.
### Are there any user-facing changes?
Yes. Readers now return an error for IPC schemas with an out-of-range
decimal precision instead of accepting them.
---
arrow/ipc/fuzz_regression_test.go | 4 +---
arrow/ipc/metadata.go | 8 ++++----
arrow/ipc/metadata_test.go | 23 +++++++++++++++++++++++
3 files changed, 28 insertions(+), 7 deletions(-)
diff --git a/arrow/ipc/fuzz_regression_test.go
b/arrow/ipc/fuzz_regression_test.go
index 828e6186..2406f3c8 100644
--- a/arrow/ipc/fuzz_regression_test.go
+++ b/arrow/ipc/fuzz_regression_test.go
@@ -37,11 +37,9 @@ func TestStreamFuzzRegression(t *testing.T) {
t.Fatal(err)
}
- // Go reads these to the end without error or batches; Arrow C++
rejects both
- // (invalid flatbuffer, decimal precision 0), so tightening validation
may flip them.
+ // Go accepts this invalid flatbuffer, which Arrow C++ rejects.
clean := map[string]bool{
"clusterfuzz-testcase-minimized-arrow-ipc-stream-fuzz-4757582821064704": true,
- "crash-5e88bae6ac5250714e8c8bc73b9d67b949fadbb4":
true,
}
count := 0
for _, entry := range entries {
diff --git a/arrow/ipc/metadata.go b/arrow/ipc/metadata.go
index f7a63915..fac5c929 100644
--- a/arrow/ipc/metadata.go
+++ b/arrow/ipc/metadata.go
@@ -980,13 +980,13 @@ func floatToFB(b *flatbuffers.Builder, bw int32)
flatbuffers.UOffsetT {
func decimalFromFB(data flatbuf.Decimal) (arrow.DataType, error) {
switch data.BitWidth() {
case 32:
- return &arrow.Decimal32Type{Precision: data.Precision(), Scale:
data.Scale()}, nil
+ return arrow.NewDecimalType(arrow.DECIMAL32, data.Precision(),
data.Scale())
case 64:
- return &arrow.Decimal64Type{Precision: data.Precision(), Scale:
data.Scale()}, nil
+ return arrow.NewDecimalType(arrow.DECIMAL64, data.Precision(),
data.Scale())
case 128:
- return &arrow.Decimal128Type{Precision: data.Precision(),
Scale: data.Scale()}, nil
+ return arrow.NewDecimalType(arrow.DECIMAL128, data.Precision(),
data.Scale())
case 256:
- return &arrow.Decimal256Type{Precision: data.Precision(),
Scale: data.Scale()}, nil
+ return arrow.NewDecimalType(arrow.DECIMAL256, data.Precision(),
data.Scale())
default:
return nil, fmt.Errorf("arrow/ipc: invalid decimal bitwidth:
%d", data.BitWidth())
}
diff --git a/arrow/ipc/metadata_test.go b/arrow/ipc/metadata_test.go
index ac8820dc..14e9de41 100644
--- a/arrow/ipc/metadata_test.go
+++ b/arrow/ipc/metadata_test.go
@@ -266,3 +266,26 @@ func TestUnrecognizedExtensionType(t *testing.T) {
assert.Truef(t, array.RecordEqual(rec, batchNoExt), "expected: %s\ngot:
%s\n", batchNoExt, rec)
}
+
+func TestDecimalFromFBPrecision(t *testing.T) {
+ for _, tc := range []struct{ width, max int32 }{{32, 9}, {64, 18},
{128, 38}, {256, 76}} {
+ for _, precision := range []int32{-1, 0, 1, tc.max, tc.max + 1}
{
+ b := flatbuffers.NewBuilder(0)
+ flatbuf.DecimalStart(b)
+ flatbuf.DecimalAddBitWidth(b, tc.width)
+ flatbuf.DecimalAddPrecision(b, precision)
+ flatbuf.DecimalAddScale(b, -2)
+ b.Finish(flatbuf.DecimalEnd(b))
+ data := flatbuf.GetRootAsDecimal(b.FinishedBytes(), 0)
+ got, err := decimalFromFB(*data)
+ if precision < 1 || precision > tc.max {
+ assert.Error(t, err, "width=%d precision=%d",
tc.width, precision)
+ } else if assert.NoError(t, err, "width=%d
precision=%d", tc.width, precision) {
+ decimal := got.(arrow.DecimalType)
+ assert.Equal(t, int(tc.width),
decimal.BitWidth())
+ assert.Equal(t, precision,
decimal.GetPrecision())
+ assert.Equal(t, int32(-2), decimal.GetScale())
+ }
+ }
+ }
+}