Skip to content

Commit 12afac8

Browse files
authored
Merge pull request #66 from msgpack/fix_range_error_on_encoding_blob
fix RangeError in encoding BLOB
2 parents dc24384 + d16cfdc commit 12afac8

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/Encoder.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export class Encoder {
8080
this.writeU8(object);
8181
} else if (object < 0x100) {
8282
// uint 8
83-
this.writeU8v(0xcc, object);
83+
this.writeU8(0xcc);
84+
this.writeU8(object);
8485
} else if (object < 0x10000) {
8586
// uint 16
8687
this.writeU8(0xcd);
@@ -200,7 +201,7 @@ export class Encoder {
200201
throw new Error(`Too large binary: ${size}`);
201202
}
202203
const bytes = ensureUint8Array(object);
203-
this.writeU8v(...bytes);
204+
this.writeU8a(bytes);
204205
}
205206

206207
encodeArray(object: Array<unknown>, depth: number) {
@@ -285,7 +286,7 @@ export class Encoder {
285286
throw new Error(`Too large extension object: ${size}`);
286287
}
287288
this.writeI8(ext.type);
288-
this.writeU8v(...ext.data);
289+
this.writeU8a(ext.data);
289290
}
290291

291292
writeU8(value: number) {
@@ -295,14 +296,11 @@ export class Encoder {
295296
this.pos++;
296297
}
297298

298-
writeU8v(...values: Array<number>) {
299+
writeU8a(values: ArrayLike<number>) {
299300
const size = values.length;
300301
this.ensureBufferSizeToWrite(size);
301302

302-
const pos = this.pos;
303-
for (let i = 0; i < size; i++) {
304-
this.view.setUint8(pos + i, values[i]);
305-
}
303+
this.bytes.set(values, this.pos);
306304
this.pos += size;
307305
}
308306

test/msgpack-test-suite.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ describe("msgpack-test-suite", () => {
9696
STR_INCLUDING_NUL: "foo\0bar\0",
9797
STR_BROKEN_FF: "\xff",
9898
BIN16: new Uint8Array(0x100).fill(0xff),
99-
BIN32: new Uint8Array(0x10000).fill(0xff),
99+
BIN32: new Uint8Array(0x10_000).fill(0xff),
100+
BIN32LARGE: new Uint8Array(0x100_000).fill(0xff), // regression: caused "RangeError: Maximum call stack size exceeded"
100101
ARRAY16: new Array<boolean>(0x100).fill(true),
101102
ARRAY32: new Array<boolean>(0x10000).fill(true),
102103
MAP16: new Array<null>(0x100).fill(null).reduce<Record<string, number>>((acc, _val, i) => {

0 commit comments

Comments
 (0)