From ea409a72d46e857c423095d9e72a42de28adb74d Mon Sep 17 00:00:00 2001 From: Sealdot <48669444+Sealdot@users.noreply.github.com> Date: Thu, 3 Dec 2026 08:20:20 +0800 Subject: [PATCH] fix: Reject variable-width columns whose value offsets are out of bounds Throw when the value offsets of a Utf8, LargeUtf8, Binary, or LargeBinary column are fewer than length + 1, negative, out of order, or end past the data buffer, instead of reading clamped or overlapping values. AI assistance: Cognition Devin CLI agent (SWE-2 High). --- src/visitor/vectorloader.ts | 52 ++++++++- .../ipc/reader/variable-width-offset-tests.ts | 105 ++++++++++++++++++ 2 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 test/unit/ipc/reader/variable-width-offset-tests.ts diff --git a/src/visitor/vectorloader.ts b/src/visitor/vectorloader.ts index 4329cf10..b2229eb0 100644 --- a/src/visitor/vectorloader.ts +++ b/src/visitor/vectorloader.ts @@ -73,10 +73,18 @@ export class VectorLoader extends Visitor { return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), data: this.readData(type) }); } public visitUtf8(type: T, { length, nullCount } = this.nextFieldNode()) { - return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) }); + const nullBitmap = this.readNullBitmap(type, nullCount); + const valueOffsets = this.readOffsets(type); + const data = this.readData(type); + checkValueOffsets(type, valueOffsets, data, length); + return makeData({ type, length, nullCount, nullBitmap, valueOffsets, data }); } public visitLargeUtf8(type: T, { length, nullCount } = this.nextFieldNode()) { - return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) }); + const nullBitmap = this.readNullBitmap(type, nullCount); + const valueOffsets = this.readOffsets(type); + const data = this.readData(type); + checkValueOffsets(type, valueOffsets, data, length); + return makeData({ type, length, nullCount, nullBitmap, valueOffsets, data }); } public visitUtf8View(type: T, { length, nullCount } = this.nextFieldNode()) { const nullBitmap = this.readNullBitmap(type, nullCount); @@ -92,10 +100,18 @@ export class VectorLoader extends Visitor { }); } public visitBinary(type: T, { length, nullCount } = this.nextFieldNode()) { - return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) }); + const nullBitmap = this.readNullBitmap(type, nullCount); + const valueOffsets = this.readOffsets(type); + const data = this.readData(type); + checkValueOffsets(type, valueOffsets, data, length); + return makeData({ type, length, nullCount, nullBitmap, valueOffsets, data }); } public visitLargeBinary(type: T, { length, nullCount } = this.nextFieldNode()) { - return makeData({ type, length, nullCount, nullBitmap: this.readNullBitmap(type, nullCount), valueOffsets: this.readOffsets(type), data: this.readData(type) }); + const nullBitmap = this.readNullBitmap(type, nullCount); + const valueOffsets = this.readOffsets(type); + const data = this.readData(type); + checkValueOffsets(type, valueOffsets, data, length); + return makeData({ type, length, nullCount, nullBitmap, valueOffsets, data }); } public visitBinaryView(type: T, { length, nullCount } = this.nextFieldNode()) { const nullBitmap = this.readNullBitmap(type, nullCount); @@ -340,3 +356,31 @@ export class CompressedVectorLoader extends VectorLoader { return this.bodyChunks[this.buffersIndex]; } } + +/** @ignore */ +function checkValueOffsets(type: T, valueOffsets: Uint8Array, data: Uint8Array, length: number) { + const width = type.OffsetArrayType.BYTES_PER_ELEMENT; + const count = Math.trunc(valueOffsets.byteLength / width); + if (count < length + 1) { + if (length === 0 && count === 0) { return; } + throw new Error(`Invalid value offsets for a column of type ${type}: the buffer holds ${count} offsets but ${length + 1} are needed`); + } + const view = new DataView(valueOffsets.buffer, valueOffsets.byteOffset, count * width); + const offsetAt = width === 8 + ? (index: number) => view.getBigInt64(index * 8, true) + : (index: number) => BigInt(view.getInt32(index * 4, true)); + let previous = offsetAt(0); + if (previous < 0n) { + throw new Error(`Invalid value offsets for a column of type ${type}: the first offset is ${previous}, below zero`); + } + for (let index = 1; index <= length; ++index) { + const offset = offsetAt(index); + if (offset < previous) { + throw new Error(`Invalid value offsets for a column of type ${type}: offset ${index} is ${offset}, below offset ${index - 1} which is ${previous}`); + } + previous = offset; + } + if (previous > BigInt(data.byteLength)) { + throw new Error(`Invalid value offsets for a column of type ${type}: the last offset is ${previous}, past the ${data.byteLength}-byte data buffer`); + } +} diff --git a/test/unit/ipc/reader/variable-width-offset-tests.ts b/test/unit/ipc/reader/variable-width-offset-tests.ts new file mode 100644 index 00000000..d169fd84 --- /dev/null +++ b/test/unit/ipc/reader/variable-width-offset-tests.ts @@ -0,0 +1,105 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import { + Binary, + LargeUtf8, + RecordBatchFileWriter, + RecordBatchReader, + RecordBatchStreamWriter, + Table, + Utf8, + vectorFromArray +} from 'apache-arrow'; + +const placeOf = (bytes: Uint8Array, pattern: Uint8Array) => { + const places: number[] = []; + for (let at = 0; at + pattern.length <= bytes.length; ++at) { + if (pattern.every((byte, index) => bytes[at + index] === byte)) places.push(at); + } + if (places.length !== 1) throw new Error(`expected 1 location of the offsets, found ${places.length}`); + return places[0]; +}; + +const int32Offsets = (values: number[]) => new Uint8Array(new Int32Array(values).buffer); +const int64Offsets = (values: number[]) => new Uint8Array(new BigInt64Array(values.map(BigInt)).buffer); + +describe('a variable-width column whose value offsets are out of bounds', () => { + + const table = new Table({ s: vectorFromArray(['a', 'bb', 'ccc', 'dddd'], new Utf8()) }); + + // The value offsets of s are [0, 1, 3, 6, 10] as little-endian int32 values; the data buffer holds 10 bytes. + const withOffset = (bytes: Uint8Array, index: number, value: number) => { + const copy = bytes.slice(); + new DataView(copy.buffer, copy.byteOffset).setInt32(placeOf(copy, int32Offsets([0, 1, 3, 6, 10])) + 4 * index, value, true); + return copy; + }; + + for (const [format, bytes] of [ + ['stream', RecordBatchStreamWriter.writeAll(table).toUint8Array(true)], + ['file', RecordBatchFileWriter.writeAll(table).toUint8Array(true)] + ]) { + test(`rejects a ${format} whose last offset is 8 bytes past the data buffer`, () => { + expect(() => [...RecordBatchReader.from(withOffset(bytes, 4, 18))]) + .toThrow(/last offset is 18.*data buffer/i); + }); + + test(`rejects a ${format} whose third offset is below the second`, () => { + expect(() => [...RecordBatchReader.from(withOffset(bytes, 3, 2))]) + .toThrow(/offset 3 is 2.*below offset 2/i); + }); + + test(`rejects a ${format} whose first offset is negative`, () => { + expect(() => [...RecordBatchReader.from(withOffset(bytes, 0, -4))]) + .toThrow(/first offset is -4.*below zero/i); + }); + } + + test('rejects a Binary column whose last offset is past the data buffer', () => { + const binary = new Table({ b: vectorFromArray([new Uint8Array([0xAA]), new Uint8Array([0xBB, 0xCC])], new Binary()) }); + const bytes = RecordBatchStreamWriter.writeAll(binary).toUint8Array(true); + // The value offsets of b are [0, 1, 3]; the data buffer holds 3 bytes. + const copy = bytes.slice(); + new DataView(copy.buffer, copy.byteOffset).setInt32(placeOf(copy, int32Offsets([0, 1, 3])) + 8, 9, true); + expect(() => [...RecordBatchReader.from(copy)]).toThrow(/last offset is 9.*data buffer/i); + }); + + test('rejects a LargeUtf8 column whose offsets are not monotonic', () => { + const large = new Table({ l: vectorFromArray(['a', 'bb', 'ccc'], new LargeUtf8()) }); + const bytes = RecordBatchStreamWriter.writeAll(large).toUint8Array(true); + // The value offsets of l are [0, 1, 3, 6] as little-endian int64 values. + const copy = bytes.slice(); + new DataView(copy.buffer, copy.byteOffset).setBigInt64(placeOf(copy, int64Offsets([0, 1, 3, 6])) + 16, 0n, true); + expect(() => [...RecordBatchReader.from(copy)]).toThrow(/offset 2 is 0.*below offset 1/i); + }); + + for (const [format, bytes] of [ + ['stream', RecordBatchStreamWriter.writeAll(table).toUint8Array(true)], + ['file', RecordBatchFileWriter.writeAll(table).toUint8Array(true)] + ]) { + test(`still reads a valid ${format}`, () => { + const [batch] = [...RecordBatchReader.from(bytes)]; + expect(Array.from(batch.getChild('s')!)).toEqual(['a', 'bb', 'ccc', 'dddd']); + }); + } + + test('still reads a column with empty strings and repeated offsets', () => { + const empties = new Table({ s: vectorFromArray(['', 'a', '', 'bb', ''], new Utf8()) }); + const [batch] = [...RecordBatchReader.from(RecordBatchStreamWriter.writeAll(empties).toUint8Array(true))]; + expect(Array.from(batch.getChild('s')!)).toEqual(['', 'a', '', 'bb', '']); + }); +});