From 4cf1611642d3f84498556db0fa06597f47bdc8d1 Mon Sep 17 00:00:00 2001 From: xdnw Date: Sun, 4 Feb 2024 11:44:25 +0000 Subject: [PATCH 1/2] Fix varint read overflow in larger number ranges. Reading varlongs will still fail for larger values --- dist/bytebuffer.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/dist/bytebuffer.js b/dist/bytebuffer.js index 8362fa3..8ee4625 100644 --- a/dist/bytebuffer.js +++ b/dist/bytebuffer.js @@ -1578,6 +1578,9 @@ * @expose */ ByteBuffer.zigZagDecode32 = function(n) { + if (n > 0x7fffffff) { + return Math.round(Math.floor(n / 2) - (n % 2 != 0 ? 1 : 0)); + } return ((n >>> 1) ^ -(n & 1)) | 0; // // ref: src/google/protobuf/wire_format_lite.h }; @@ -1665,11 +1668,19 @@ throw err; } b = this.view[offset++]; - if (c < 5) + if (c < 4) { value |= (b & 0x7f) << (7*c); + } else if (c < 10) { + let valCopy = Number(value); + value += (b & 0x7f) * Math.pow(2, 7*c); + } ++c; } while ((b & 0x80) !== 0); - value |= 0; + if (value <= 2147483647) { + value |= 0; + } else { + value = Math.floor(value); + } if (relative) { this.offset = offset; return value; From 917cfbe41ed502655c34faa52f9d89baaaf69980 Mon Sep 17 00:00:00 2001 From: xdnw Date: Sun, 4 Feb 2024 11:46:02 +0000 Subject: [PATCH 2/2] Update bytebuffer.js --- dist/bytebuffer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/dist/bytebuffer.js b/dist/bytebuffer.js index 8ee4625..ec2cf02 100644 --- a/dist/bytebuffer.js +++ b/dist/bytebuffer.js @@ -1671,7 +1671,6 @@ if (c < 4) { value |= (b & 0x7f) << (7*c); } else if (c < 10) { - let valCopy = Number(value); value += (b & 0x7f) * Math.pow(2, 7*c); } ++c;