Skip to content

Commit

Permalink
Fixed isBytes check for invalid length or elements (#1964).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 4, 2021
1 parent f8adf82 commit 7a404fb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/bytes/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,20 @@ export function isBytesLike(value: any): value is BytesLike {
return ((isHexString(value) && !(value.length % 2)) || isBytes(value));
}

function isInteger(value: number) {
return (typeof(value) === "number" && value == value && (value % 1) === 0);
}

export function isBytes(value: any): value is Bytes {
if (value == null) { return false; }

if (value.constructor === Uint8Array) { return true; }
if (typeof(value) === "string") { return false; }
if (value.length == null) { return false; }
if (!isInteger(value.length) || value.length < 0) { return false; }

for (let i = 0; i < value.length; i++) {
const v = value[i];
if (typeof(v) !== "number" || v < 0 || v >= 256 || (v % 1)) {
return false;
}
if (!isInteger(v) || v < 0 || v >= 256) { return false; }
}
return true;
}
Expand Down

0 comments on commit 7a404fb

Please sign in to comment.