Skip to content

Commit b4c67b3

Browse files
committed
fix: add validation for binary vector in Binary toXArray methods
1 parent 0f7e86c commit b4c67b3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/binary.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ export class Binary extends BSONValue {
341341
throw new BSONError('Binary datatype field is not Int8');
342342
}
343343

344+
validateBinaryVector(this);
345+
344346
return new Int8Array(
345347
this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
346348
);
@@ -361,6 +363,8 @@ export class Binary extends BSONValue {
361363
throw new BSONError('Binary datatype field is not Float32');
362364
}
363365

366+
validateBinaryVector(this);
367+
364368
const floatBytes = new Uint8Array(
365369
this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
366370
);
@@ -387,6 +391,8 @@ export class Binary extends BSONValue {
387391
throw new BSONError('Binary datatype field is not packed bit');
388392
}
389393

394+
validateBinaryVector(this);
395+
390396
return new Uint8Array(
391397
this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
392398
);

test/node/bson_binary_vector.spec.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ function testVectorInvalidBSONBytes(test: VectorTest, expectedErrorMessage: stri
172172
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
173173
});
174174

175+
const toHelper = dtypeToHelper(test.dtype_hex).replace('from', 'to');
176+
it(`Binary.${toHelper}() throw a BSONError`, function () {
177+
let thrownError: Error | undefined;
178+
const bin = BSON.deserialize(Buffer.from(test.canonical_bson!, 'hex'));
179+
180+
try {
181+
bin.vector[toHelper]();
182+
} catch (error) {
183+
thrownError = error;
184+
}
185+
186+
expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError);
187+
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
188+
});
189+
175190
it(`EJSON.stringify() throw a BSONError`, function () {
176191
let thrownError: Error | undefined;
177192
const bin = BSON.deserialize(Buffer.from(test.canonical_bson!, 'hex'));

0 commit comments

Comments
 (0)