java-topology/defects/jitsi-videobridge/patch/0001-prioritize-hashset-contains-indexOf.patch

37 lines
2.1 KiB
Diff

# UNDF: UNDF-2026-000000127
diff --git a/jvb/src/main/kotlin/org/jitsi/videobridge/cc/allocation/Prioritize.kt b/jvb/src/main/kotlin/org/jitsi/videobridge/cc/allocation/Prioritize.kt
index 1234567..abcdef0 100644
--- a/jvb/src/main/kotlin/org/jitsi/videobridge/cc/allocation/Prioritize.kt
+++ b/jvb/src/main/kotlin/org/jitsi/videobridge/cc/allocation/Prioritize.kt
@@ -32,16 +32,20 @@ fun prioritize(
val enabledSelectedSources = mutableListOf<MediaSourceDesc>()
val enabledNonSelectedSources = mutableListOf<MediaSourceDesc>()
val disabledSources = mutableListOf<MediaSourceDesc>()
+ // CWE-407 fix: pre-build O(1) lookup structures before iterating conferenceSources
+ val selectedSet = selectedSourceNames.toHashSet()
+ val selectedIndex = selectedSourceNames.withIndex().associate { (i, s) -> s to i }
+
// conferenceSources can be large, while selectedSourceNames is usually small, so do a single pass over
// conferenceSources.
conferenceSources.forEach { source ->
if (source.videoType.isEnabled()) {
- if (selectedSourceNames.contains(source.sourceName)) {
+ if (selectedSet.contains(source.sourceName)) {
enabledSelectedSources.add(source)
} else {
enabledNonSelectedSources.add(source)
}
} else {
disabledSources.add(source)
}
}
- // The enabled selected sources are sorted according to the order in which they are selected and prioritized
- // over non-selected.
- enabledSelectedSources.sortBy { selectedSourceNames.indexOf(it.sourceName) }
+ // The enabled selected sources are sorted according to the order in which they are selected and prioritized
+ // over non-selected. Use pre-built index map for O(1) lookup instead of O(n) indexOf.
+ enabledSelectedSources.sortBy { selectedIndex.getOrDefault(it.sourceName, Int.MAX_VALUE) }
enabledSelectedSources.addAll(enabledNonSelectedSources)
// All disabled sources are sorted last, regardless of whether they are selected.
enabledSelectedSources.addAll(disabledSources)