Skip to content

Commit

Permalink
fix: Overflow when spliting one 64-bit Integer into two 32-bit Integers
Browse files Browse the repository at this point in the history
  • Loading branch information
roy committed Sep 24, 2024
1 parent 0ac298a commit 4a25468
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions websocket-sftp/lib/sftp-packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4a25468

Please sign in to comment.