From 4a25468ce1effeebebadb6e581d56b81623a4fd2 Mon Sep 17 00:00:00 2001 From: roy Date: Tue, 24 Sep 2024 21:50:07 +0800 Subject: [PATCH] fix: Overflow when spliting one 64-bit Integer into two 32-bit Integers --- websocket-sftp/lib/sftp-packet.ts | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/websocket-sftp/lib/sftp-packet.ts b/websocket-sftp/lib/sftp-packet.ts index f0ab648..85e076a 100644 --- a/websocket-sftp/lib/sftp-packet.ts +++ b/websocket-sftp/lib/sftp-packet.ts @@ -161,18 +161,17 @@ export class SftpPacketReader extends SftpPacket { } readInt64(): number { - const hi = this.readInt32(); - const lo = this.readUInt32(); - - const value = hi * 0x100000000 + lo; - return value; + this.check(8); + const value = this.buffer.readBigInt64BE(this.position); + this.position += 8; + return Number(value); } readUInt64(): number { - const hi = this.readUInt32(); - const lo = this.readUInt32(); - const value = hi * 0x100000000 + lo; - return value; + this.check(8); + const value = this.buffer.readBigUint64BE(this.position); + this.position += 8; + return Number(value); } readString(): string { @@ -271,17 +270,15 @@ export class SftpPacketWriter extends SftpPacket { } writeInt64(value: number): void { - const hi = (value / 0x100000000) | 0; - const lo = (value & 0xffffffff) | 0; - this.writeInt32(hi); - this.writeInt32(lo); + this.check(8); + this.buffer.writeBigInt64BE(BigInt(value), this.position); + this.position += 8; } writeUInt64(value: number): void { - const hi = (value / 0x100000000) | 0; - const lo = (value & 0xffffffff) | 0; - this.writeUInt32(hi); - this.writeUInt32(lo); + this.check(8); + this.buffer.writeBigUInt64BE(BigInt(value), this.position); + this.position += 8; } writeString(value: string): void {