diff --git a/src/detectors/builtin/argCopyMutation.ts b/src/detectors/builtin/argCopyMutation.ts index 14ee0d89..a89fcfc0 100644 --- a/src/detectors/builtin/argCopyMutation.ts +++ b/src/detectors/builtin/argCopyMutation.ts @@ -7,7 +7,7 @@ import { funName, } from "../../internals/tact"; import { hasInExpressions } from "../../internals/tact/iterators"; -import { intersection } from "../../internals/util"; +import { intersectLists } from "../../internals/util"; import { MistiTactWarning, Severity } from "../../internals/warnings"; import { ASTDetector } from "../detector"; import { @@ -202,7 +202,7 @@ export class ArgCopyMutation extends ASTDetector { ): Map { const mutations = collectMutations(stmt, { flatStmts: true }); const foundMutations = mutations - ? intersection(argNames, mutationNames(mutations.mutatedLocals)) + ? intersectLists(argNames, mutationNames(mutations.mutatedLocals)) : []; return foundMutations.reduce((mutationMap, argName) => { const existingStatements = mutationMap.get(argName) || []; diff --git a/src/detectors/builtin/inheritedStateMutation.ts b/src/detectors/builtin/inheritedStateMutation.ts index d5e82d1b..e8cadd35 100644 --- a/src/detectors/builtin/inheritedStateMutation.ts +++ b/src/detectors/builtin/inheritedStateMutation.ts @@ -5,7 +5,7 @@ import { collectMutations, mutationNames, } from "../../internals/tact"; -import { intersection } from "../../internals/util"; +import { intersectLists } from "../../internals/util"; import { MistiTactWarning, Severity } from "../../internals/warnings"; import { ASTDetector } from "../detector"; import { AstStatement, AstNode } from "@tact-lang/compiler/dist/grammar/ast"; @@ -99,7 +99,7 @@ export class InheritedStateMutation extends ASTDetector { ): MistiTactWarning[] { const mutations = collectMutations(stmt); const foundMutations = mutations - ? intersection( + ? intersectLists( inheritedFieldNames, mutationNames(mutations.mutatedFields), ) diff --git a/src/detectors/builtin/neverAccessedVariables.ts b/src/detectors/builtin/neverAccessedVariables.ts index f7821b2d..37e07a79 100644 --- a/src/detectors/builtin/neverAccessedVariables.ts +++ b/src/detectors/builtin/neverAccessedVariables.ts @@ -8,7 +8,7 @@ import { forEachExpression, } from "../../internals/tact"; import { Transfer } from "../../internals/transfer"; -import { mergeSets, isSubsetOf } from "../../internals/util"; +import { mergeSets, isSetSubsetOf } from "../../internals/util"; import { unreachable } from "../../internals/util"; import { MistiTactWarning, Severity } from "../../internals/warnings"; import { DataflowDetector, WarningsBehavior } from "../detector"; @@ -59,8 +59,8 @@ class VariableUsageLattice implements JoinSemilattice { leq(a: VariableState, b: VariableState): boolean { return ( [...a.declared.extract()].every((x) => b.declared.has(x)) && - isSubsetOf(a.accessed, b.accessed) && - isSubsetOf(a.written, b.written) + isSetSubsetOf(a.accessed, b.accessed) && + isSetSubsetOf(a.written, b.written) ); } } diff --git a/src/detectors/builtin/stringReceiversOverlap.ts b/src/detectors/builtin/stringReceiversOverlap.ts index fb4d2282..6bf40e21 100644 --- a/src/detectors/builtin/stringReceiversOverlap.ts +++ b/src/detectors/builtin/stringReceiversOverlap.ts @@ -3,7 +3,7 @@ import { JoinSemilattice } from "../../internals/lattice"; import { WorklistSolver } from "../../internals/solver/"; import { forEachExpression, forEachStatement } from "../../internals/tact"; import { Transfer } from "../../internals/transfer"; -import { mergeSets, isSubsetOf } from "../../internals/util"; +import { mergeSets, isSetSubsetOf } from "../../internals/util"; import { MistiTactWarning, Severity } from "../../internals/warnings"; import { DataflowDetector } from "../detector"; import { @@ -56,8 +56,8 @@ class TaintLattice implements JoinSemilattice { leq(a: TaintState, b: TaintState): boolean { return ( - isSubsetOf(a.argTaint, b.argTaint) && - isSubsetOf(a.literalTaint, b.literalTaint) + isSetSubsetOf(a.argTaint, b.argTaint) && + isSetSubsetOf(a.literalTaint, b.literalTaint) ); } } diff --git a/src/internals/util.ts b/src/internals/util.ts index 53d6cad9..e4e6a9d9 100644 --- a/src/internals/util.ts +++ b/src/internals/util.ts @@ -7,25 +7,32 @@ import { InternalException } from "./exceptions"; import path from "path"; -/** - * Intersection of two lists. - */ -export const intersection = (l1: T[], l2: T[]): T[] => - l1.filter((element) => l2.includes(element)); - export const mergeSets = (lhs: Set, rhs: Set): Set => new Set([...lhs, ...rhs]); -export const isSubsetOf = (lhs: Set, rhs: Set): boolean => +export const isSetSubsetOf = (lhs: Set, rhs: Set): boolean => [...lhs].every((elem) => rhs.has(elem)); +export const intersectSets = (setA: Set, setB: Set): Set => + new Set([...setA].filter((item) => setB.has(item))); export const mergeLists = (lhs: T[], rhs: T[]): T[] => [...lhs, ...rhs]; export const isListSubsetOf = (lhs: T[], rhs: T[]): boolean => lhs.every((elem) => rhs.includes(elem)); +export const intersectLists = (l1: T[], l2: T[]): T[] => + l1.filter((element) => l2.includes(element)); export const mergeMaps = (lhs: Map, rhs: Map): Map => new Map([...lhs, ...rhs]); export const isMapSubsetOf = (lhs: Map, rhs: Map): boolean => [...lhs].every(([key, value]) => rhs.has(key) && rhs.get(key) === value); +export const intersectMaps = ( + mapA: Map, + mapB: Map, +): Map => + new Map( + [...mapA].filter( + ([key, value]) => mapB.has(key) && mapB.get(key) === value, + ), + ); /** * Unreachable case for exhaustive checking.