From ada6ef6ccdde08fe6b25cf8124f8967c2f86a4d1 Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Sun, 15 Jan 2023 17:15:59 +0100 Subject: [PATCH] add upper bounds for u64 number since numbers can be larger than MAX_SAFE_INTEGER --- benchmark/u64.ts | 8 ++++---- src/bigint.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/benchmark/u64.ts b/benchmark/u64.ts index e0c2cacc..b586a687 100644 --- a/benchmark/u64.ts +++ b/benchmark/u64.ts @@ -8,10 +8,10 @@ import protobuf from "protobufjs"; // This benchmark is not that good since protobufjs convert bigints into LongBit format rather than bigint on deserialization /* -* borsh bigint x 5,326,970 ops/sec ±0.37% (395 runs sampled) -* protobujs bigint x 6,904,183 ops/sec ±0.42% (392 runs sampled) -* borsh number x 6,350,140 ops/sec ±0.30% (394 runs sampled) -* protobujs number x 5,152,453 ops/sec ±0.41% (394 runs sampled) +* borsh bigint x 5,385,093 ops/sec ±0.44% (389 runs sampled) +* protobujs bigint x 7,022,500 ops/sec ±0.53% (393 runs sampled) +* borsh number x 6,124,037 ops/sec ±0.42% (393 runs sampled) +* protobujs number x 4,639,581 ops/sec ±0.35% (394 runs sampled) */ function getRandomInt(max: number) { diff --git a/src/bigint.ts b/src/bigint.ts index 7178b41c..9f200df3 100644 --- a/src/bigint.ts +++ b/src/bigint.ts @@ -57,7 +57,7 @@ export const writeBigUint64Le = (bigIntOrNumber: bigint | number, buf: Uint8Arra } else { - if (bigIntOrNumber < 0) { + if (bigIntOrNumber < 0 || bigIntOrNumber > 18446744073709551615n) { throw new Error("u64 value can not negative, got " + bigIntOrNumber) } // We don't need upper bound check because number can not exceed 18446744073709551615