@@ -45,6 +45,14 @@ export type BSONElement = [
45
45
length : number
46
46
] ;
47
47
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
+
48
56
/**
49
57
* Searches for null terminator of a BSON element's value (Never the document null terminator)
50
58
* **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(
80
88
) ;
81
89
}
82
90
83
- const documentSize = NumberUtils . getSize ( bytes , startOffset ) ;
91
+ const documentSize = getSize ( bytes , startOffset ) ;
84
92
85
93
if ( documentSize > bytes . length - startOffset ) {
86
94
throw new BSONOffsetError (
@@ -144,15 +152,15 @@ export function parseToElements(
144
152
type === BSONElementType . array ||
145
153
type === BSONElementType . javascriptWithScope
146
154
) {
147
- length = NumberUtils . getSize ( bytes , offset ) ;
155
+ length = getSize ( bytes , offset ) ;
148
156
} else if (
149
157
type === BSONElementType . string ||
150
158
type === BSONElementType . binData ||
151
159
type === BSONElementType . dbPointer ||
152
160
type === BSONElementType . javascript ||
153
161
type === BSONElementType . symbol
154
162
) {
155
- length = NumberUtils . getSize ( bytes , offset ) + 4 ;
163
+ length = getSize ( bytes , offset ) + 4 ;
156
164
if ( type === BSONElementType . binData ) {
157
165
// binary subtype
158
166
length += 1 ;
0 commit comments