Skip to content

Commit cdc985d

Browse files
authored
Stable ordering for flows (#1004)
* Ensure stable order for data flow results * Retrigger build
1 parent 1626782 commit cdc985d

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,3 @@ A metadata block (type META_DATA) is included in CPG with two fields: a language
271271

272272
[3] The ShiftLeft Tinkergraph
273273
https://github.com/ShiftLeftSecurity/tinkergraph-gremlin
274-

dataflowengineoss/src/main/scala/io/shiftleft/dataflowengineoss/queryengine/Engine.scala

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Engine(context: EngineContext) {
6464
logger.warn(exception.getMessage)
6565
}
6666
}
67-
result
67+
deduplicate(result.toVector).toList
6868
}
6969

7070
private def newTasksFromResults(resultsOfTask: Vector[ReachableByResult],
@@ -214,6 +214,30 @@ object Engine {
214214
.argument(param.order)
215215
.l
216216

217+
def deduplicate(vec: Vector[ReachableByResult]): Vector[ReachableByResult] = {
218+
vec
219+
.groupBy { x =>
220+
(x.path.headOption ++ x.path.lastOption, x.partial, x.callDepth)
221+
}
222+
.map {
223+
case (_, list) =>
224+
val lenIdPathPairs = list.map(x => (x.path.length, x)).toList
225+
val withMaxLength = (lenIdPathPairs.sortBy(_._1).reverse match {
226+
case Nil => Nil
227+
case h :: t => h :: t.takeWhile(y => y._1 == h._1)
228+
}).map(_._2)
229+
230+
if (withMaxLength.length == 1) {
231+
withMaxLength.head
232+
} else {
233+
withMaxLength.minBy { x =>
234+
x.path.map(_.node.id()).mkString("-")
235+
}
236+
}
237+
}
238+
.toVector
239+
}
240+
217241
}
218242

219243
case class EngineContext(semantics: Semantics, config: EngineConfig = EngineConfig())
@@ -301,28 +325,7 @@ private class ReachableByCallable(task: ReachableByTask, context: EngineContext)
301325
endStates ++ retsToResolve
302326
}
303327

304-
val res = (resultsForParents ++ resultsForCurNode)
305-
.groupBy { x =>
306-
(x.path.headOption ++ x.path.lastOption, x.partial, x.callDepth)
307-
}
308-
.map {
309-
case (_, list) =>
310-
val lenIdPathPairs = list.map(x => (x.path.length, x)).toList
311-
val withMaxLength = (lenIdPathPairs.sortBy(_._1).reverse match {
312-
case Nil => Nil
313-
case h :: t => h :: t.takeWhile(y => y._1 == h._1)
314-
}).map(_._2)
315-
316-
if (withMaxLength.length == 1) {
317-
withMaxLength.head
318-
} else {
319-
withMaxLength.minBy { x =>
320-
x.path.map(_.node.id()).mkString("-")
321-
}
322-
}
323-
}
324-
.toVector
325-
328+
val res = deduplicate(resultsForParents ++ resultsForCurNode)
326329
table.add(curNode, res)
327330
res
328331
}

0 commit comments

Comments
 (0)