Skip to content

Commit 7a404fb

Browse files
committed
Fixed isBytes check for invalid length or elements (#1964).
1 parent f8adf82 commit 7a404fb

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

packages/bytes/src.ts/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,20 @@ export function isBytesLike(value: any): value is BytesLike {
7070
return ((isHexString(value) && !(value.length % 2)) || isBytes(value));
7171
}
7272

73+
function isInteger(value: number) {
74+
return (typeof(value) === "number" && value == value && (value % 1) === 0);
75+
}
76+
7377
export function isBytes(value: any): value is Bytes {
7478
if (value == null) { return false; }
7579

7680
if (value.constructor === Uint8Array) { return true; }
7781
if (typeof(value) === "string") { return false; }
78-
if (value.length == null) { return false; }
82+
if (!isInteger(value.length) || value.length < 0) { return false; }
7983

8084
for (let i = 0; i < value.length; i++) {
8185
const v = value[i];
82-
if (typeof(v) !== "number" || v < 0 || v >= 256 || (v % 1)) {
83-
return false;
84-
}
86+
if (!isInteger(v) || v < 0 || v >= 256) { return false; }
8587
}
8688
return true;
8789
}

0 commit comments

Comments
 (0)