Skip to content

Commit

Permalink
Merge pull request #35 from Trinketer22/safe_int
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlkv authored Aug 9, 2024
2 parents 2cd5401 + 54f3abd commit b5aa938
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/boc/BitReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class BitReader {
* @returns read value as number
*/
loadUint(bits: number): number {
return Number(this.loadUintBig(bits));
return this._toSafeInteger(this.loadUintBig(bits), 'loadUintBig');
}

/**
Expand All @@ -151,7 +151,7 @@ export class BitReader {
* @returns read value as number
*/
preloadUint(bits: number): number {
return Number(this._preloadUint(bits, this._offset));
return this._toSafeInteger(this._preloadUint(bits, this._offset), 'preloadUintBig');
}

/**
Expand All @@ -171,7 +171,7 @@ export class BitReader {
loadInt(bits: number): number {
let res = this._preloadInt(bits, this._offset);
this._offset += bits;
return Number(res);
return this._toSafeInteger(res, 'loadUintBig');
}

/**
Expand All @@ -191,7 +191,7 @@ export class BitReader {
* @returns read value as bigint
*/
preloadInt(bits: number): number {
return Number(this._preloadInt(bits, this._offset));
return this._toSafeInteger(this._preloadInt(bits, this._offset), 'preloadIntBig');
}

/**
Expand All @@ -210,7 +210,7 @@ export class BitReader {
*/
loadVarUint(bits: number): number {
let size = Number(this.loadUint(bits));
return Number(this.loadUintBig(size * 8));
return this._toSafeInteger(this.loadUintBig(size * 8), 'loadVarUintBig');
}

/**
Expand All @@ -230,7 +230,7 @@ export class BitReader {
*/
preloadVarUint(bits: number): number {
let size = Number(this._preloadUint(bits, this._offset));
return Number(this._preloadUint(size * 8, this._offset + bits));
return this._toSafeInteger(this._preloadUint(size * 8, this._offset + bits), 'preloadVarUintBig');
}

/**
Expand All @@ -250,7 +250,7 @@ export class BitReader {
*/
loadVarInt(bits: number): number {
let size = Number(this.loadUint(bits));
return Number(this.loadIntBig(size * 8));
return this._toSafeInteger(this.loadIntBig(size * 8), 'loadVarIntBig');
}

/**
Expand All @@ -270,7 +270,7 @@ export class BitReader {
*/
preloadVarInt(bits: number): number {
let size = Number(this._preloadUint(bits, this._offset));
return Number(this._preloadInt(size * 8, this._offset + bits));
return this._toSafeInteger(this._preloadInt(size * 8, this._offset + bits), 'preloadVarIntBig');
}

/**
Expand Down Expand Up @@ -509,4 +509,12 @@ export class BitReader {

return new ExternalAddress(value, bits);
}
}

private _toSafeInteger(src: bigint, alt: string) {
if(BigInt(Number.MAX_SAFE_INTEGER) < src || src < BigInt(Number.MIN_SAFE_INTEGER)) {
throw new TypeError(`${src} is out of safe integer range. Use ${alt} instead`);
}

return Number(src);
}
}

0 comments on commit b5aa938

Please sign in to comment.