30 lines
1.4 KiB
Diff
30 lines
1.4 KiB
Diff
# UNDF: UNDF-2026-000000169
|
|
--- a/networkx/algorithms/connectivity/kcutsets.py
|
|
+++ b/networkx/algorithms/connectivity/kcutsets.py
|
|
@@ -100,8 +100,12 @@ def all_node_cuts(G, k=None, flow_func=None):
|
|
# Initialize data structures.
|
|
# Keep track of the cuts already computed so we do not repeat them.
|
|
- seen = []
|
|
+ # CWE-407 fix: `not in seen` was O(K) where K is the number of cuts
|
|
+ # found so far (list scan). Each iteration in the triple-nested loop
|
|
+ # (for x in X: for v in non_adjacent: for antichain in antichains(L):)
|
|
+ # pays this cost. Fix: use a set of frozensets for O(1) lookup.
|
|
+ # node_cut is a plain set so we freeze before inserting/checking.
|
|
+ seen = set()
|
|
...
|
|
# Check if X is a k-node-cutset
|
|
if _is_separating_set(G, X):
|
|
- seen.append(X)
|
|
+ seen.add(frozenset(X))
|
|
yield X
|
|
...
|
|
# Inside the triple-nested loop:
|
|
if len(node_cut) == k:
|
|
if x in node_cut or v in node_cut:
|
|
continue
|
|
- if node_cut not in seen:
|
|
+ frozen_cut = frozenset(node_cut)
|
|
+ if frozen_cut not in seen: # O(1) hash lookup
|
|
yield node_cut
|
|
- seen.append(node_cut)
|
|
+ seen.add(frozen_cut) # O(1) insert
|