@@ -45,6 +45,14 @@ export type BSONElement = [
4545 length : number
4646] ;
4747
48+ const getSize = ( source : Uint8Array , offset : number ) => {
49+ try {
50+ return NumberUtils . getSize ( source , offset ) ;
51+ } catch ( cause ) {
52+ throw new BSONOffsetError ( 'BSON size cannot be negative' , offset , { cause } ) ;
53+ }
54+ } ;
55+
4856/**
4957 * Searches for null terminator of a BSON element's value (Never the document null terminator)
5058 * **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`.
@@ -80,7 +88,7 @@ export function parseToElements(
8088 ) ;
8189 }
8290
83- const documentSize = NumberUtils . getSize ( bytes , startOffset ) ;
91+ const documentSize = getSize ( bytes , startOffset ) ;
8492
8593 if ( documentSize > bytes . length - startOffset ) {
8694 throw new BSONOffsetError (
@@ -144,15 +152,15 @@ export function parseToElements(
144152 type === BSONElementType . array ||
145153 type === BSONElementType . javascriptWithScope
146154 ) {
147- length = NumberUtils . getSize ( bytes , offset ) ;
155+ length = getSize ( bytes , offset ) ;
148156 } else if (
149157 type === BSONElementType . string ||
150158 type === BSONElementType . binData ||
151159 type === BSONElementType . dbPointer ||
152160 type === BSONElementType . javascript ||
153161 type === BSONElementType . symbol
154162 ) {
155- length = NumberUtils . getSize ( bytes , offset ) + 4 ;
163+ length = getSize ( bytes , offset ) + 4 ;
156164 if ( type === BSONElementType . binData ) {
157165 // binary subtype
158166 length += 1 ;
0 commit comments