Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions src/visitor/vectorloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends type.Utf8>(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<T extends type.LargeUtf8>(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<T extends type.Utf8View>(type: T, { length, nullCount } = this.nextFieldNode()) {
const nullBitmap = this.readNullBitmap(type, nullCount);
Expand All @@ -92,10 +100,18 @@ export class VectorLoader extends Visitor {
});
}
public visitBinary<T extends type.Binary>(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<T extends type.LargeBinary>(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<T extends type.BinaryView>(type: T, { length, nullCount } = this.nextFieldNode()) {
const nullBitmap = this.readNullBitmap(type, nullCount);
Expand Down Expand Up @@ -340,3 +356,31 @@ export class CompressedVectorLoader extends VectorLoader {
return this.bodyChunks[this.buffersIndex];
}
}

/** @ignore */
function checkValueOffsets<T extends type.Utf8 | type.LargeUtf8 | type.Binary | type.LargeBinary>(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`);
}
}
105 changes: 105 additions & 0 deletions test/unit/ipc/reader/variable-width-offset-tests.ts
Original file line number Diff line number Diff line change
@@ -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 <const>[
['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 <const>[
['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', '']);
});
});