From 2392e3c7e2c482eb6bbdf333243ba93bf1c91c6f Mon Sep 17 00:00:00 2001 From: wanqifei Date: Fri, 1 Nov 2024 17:32:44 +0800 Subject: [PATCH 1/3] fix: fix wrong length number write to packet --- lib/packets/packet.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/packets/packet.js b/lib/packets/packet.js index 42241ae950..4a111180ff 100644 --- a/lib/packets/packet.js +++ b/lib/packets/packet.js @@ -830,11 +830,10 @@ class Packet { if (n === null) { return this.writeInt8(0xfb); } - // TODO: check that n is out of int precision this.writeInt8(0xfe); - this.buffer.writeUInt32LE(n, this.offset); + this.buffer.writeUInt32LE(n & 0xffffffff, this.offset); this.offset += 4; - this.buffer.writeUInt32LE(n >> 32, this.offset); + this.buffer.writeUInt32LE(Math.floor(n / 0x100000000), this.offset); this.offset += 4; return this.offset; } From 6c4d178e0efda3a64830a638d87855eed3e192d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Sat, 21 Feb 2026 18:42:43 -0300 Subject: [PATCH 2/3] Update lib/packets/packet.js --- lib/packets/packet.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/packets/packet.js b/lib/packets/packet.js index 12ec033775..7d9673298b 100644 --- a/lib/packets/packet.js +++ b/lib/packets/packet.js @@ -832,7 +832,7 @@ class Packet { return this.writeInt8(0xfb); } this.writeInt8(0xfe); - this.buffer.writeUInt32LE(n & 0xffffffff, this.offset); + this.buffer.writeUInt32LE(n >>> 0, this.offset); this.offset += 4; this.buffer.writeUInt32LE(Math.floor(n / 0x100000000), this.offset); this.offset += 4; From 408bd0440bbca0900f9759a7bf0817e330b1dda8 Mon Sep 17 00:00:00 2001 From: wellwelwel <46850407+wellwelwel@users.noreply.github.com> Date: Sat, 21 Feb 2026 18:49:28 -0300 Subject: [PATCH 3/3] ci: add tests --- .../packets/test-length-coded-number.test.mts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/unit/packets/test-length-coded-number.test.mts diff --git a/test/unit/packets/test-length-coded-number.test.mts b/test/unit/packets/test-length-coded-number.test.mts new file mode 100644 index 0000000000..0356aa3511 --- /dev/null +++ b/test/unit/packets/test-length-coded-number.test.mts @@ -0,0 +1,41 @@ +import { assert, describe, it } from 'poku'; +import Packet from '../../../lib/packets/packet.js'; + +describe('writeLengthCodedNumber / readLengthCodedNumber roundtrip', () => { + const roundtrip = (n: number): number | null => { + const size = 4 + Packet.lengthCodedNumberLength(n); + const buffer = Buffer.alloc(size); + const packet = new Packet(0, buffer, 0, size); + + packet.writeLengthCodedNumber(n); + packet.offset = 4; + + return packet.readLengthCodedNumber(); + }; + + it('should roundtrip values in the 1-byte range', () => { + assert.strictEqual(roundtrip(0), 0); + assert.strictEqual(roundtrip(250), 250); + }); + + it('should roundtrip values in the 2-byte range (0xFC tag)', () => { + assert.strictEqual(roundtrip(251), 251); + assert.strictEqual(roundtrip(0xfffe), 0xfffe); + }); + + it('should roundtrip values in the 3-byte range (0xFD tag)', () => { + assert.strictEqual(roundtrip(0xffff), 0xffff); + assert.strictEqual(roundtrip(0xfffffe), 0xfffffe); + }); + + it('should roundtrip values >= 0xFFFFFF in the 8-byte range (0xFE tag)', () => { + assert.strictEqual(roundtrip(0xffffff), 0xffffff); + assert.strictEqual(roundtrip(0x2000000), 0x2000000); // 32MB: PR #3177 + assert.strictEqual(roundtrip(0xffffffff), 0xffffffff); + }); + + it('should roundtrip values above 32-bit range', () => { + assert.strictEqual(roundtrip(0x100000001), 0x100000001); + assert.strictEqual(roundtrip(0x1ffffffffff), 2199023255551); + }); +});