Skip to content

Commit 29a62a6

Browse files
committed
fix: prevent infinite recursion in hasBinary
1 parent 7145012 commit 29a62a6

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

lib/is-binary.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ export function isBinary(obj: any) {
3333
);
3434
}
3535

36-
export function hasBinary(obj: any, toJSON?: boolean) {
37-
if (!obj || typeof obj !== "object") {
36+
export function hasBinary(obj: any, known: object[] = [], toJSON?: boolean) {
37+
if (!obj || typeof obj !== "object" || known.includes(obj)) {
3838
return false;
3939
}
4040

41+
known.push(obj);
42+
4143
if (Array.isArray(obj)) {
4244
for (let i = 0, l = obj.length; i < l; i++) {
43-
if (hasBinary(obj[i])) {
45+
if (hasBinary(obj[i], known)) {
4446
return true;
4547
}
4648
}
@@ -51,16 +53,15 @@ export function hasBinary(obj: any, toJSON?: boolean) {
5153
return true;
5254
}
5355

54-
if (
55-
obj.toJSON &&
56-
typeof obj.toJSON === "function" &&
57-
arguments.length === 1
58-
) {
59-
return hasBinary(obj.toJSON(), true);
56+
if (obj.toJSON && typeof obj.toJSON === "function" && arguments.length < 3) {
57+
return hasBinary(obj.toJSON(), known, true);
6058
}
6159

6260
for (const key in obj) {
63-
if (Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key])) {
61+
if (
62+
(Object.prototype.hasOwnProperty.call(obj, key) && hasBinary(obj[key]),
63+
known)
64+
) {
6465
return true;
6566
}
6667
}

0 commit comments

Comments
 (0)